How to call Finish() a separate class?

Asked

Viewed 227 times

1

I have an application where when the user clicks "Back" on MainActivity he displays a AlertDialogin the method onBackPressed() asking if you really want to quit the app. But I created a new class Gerenciar to manage, and put all methods there. The problem is that I am not able to insert the Finish() in this class and call on MainActivity. How to proceed?

1 - Code in MainActivity working:

    public void onBackPressed() {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
    builder.setTitle("Atenção!");
    builder.setMessage("Deseja realmente fechar o app?");
    builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
            finish();
        }
    });
    builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {}
    });
    alerta = builder.create();
    alerta.show();
}

2 - Code in class Gerenciar inoperative:

    public void Alertar(){
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
    builder.setTitle("Atenção!");
    builder.setMessage("Deseja realmente fechar o app?");
    builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            activity.finish();             //Finish() não aceita, e com o `activity` aceita, mas quebra a aplicação quando executa
        }
    });
    builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {}
    });
    alerta = builder.create();
    alerta.show();
}

And I call this method in MainActivity:

    public void onBackPressed() {

    final Gerenciar negGer = new Gerenciar(this);
    negGer.Alertar();
    } 

1 answer

0


You need to call the method finish() thus below:

((Activity) context).finish();

A basic example:

public class classA{

    Context context;

    public classA(Context context){
        this.context = context;
    }

    void exemploMetodo(){
        ((Activity)context).finish();
    }
}

Adapting to your code:

public void Alertar() {
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
    builder.setTitle("Atenção!");
    builder.setMessage("Deseja realmente fechar o app?");
    builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {

            ((Activity) context).finish();

        }
    });
    builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {}
    });
    alerta = builder.create();
    alerta.show();
}
  • In class Gerenciar or in the MainActivity?

  • In the class you call the Dialog.

  • @Jonasvieira changed to context, because I saw that you are calling the context in your Alertdialog. Just need this and chablauuuu...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.