How to close app when I press the native back button of android?

Asked

Viewed 373 times

1

I wanted to know how to terminate the application when I press the native back button of android am using this method:

@Override
public void onBackPressed() {


        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Atenção");
        dialog.setMessage("Tem certeza que deseja fechar o app?");
        dialog.setNegativeButton("Não", null);
        dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                System.exit(0);
                finish();
            }
        }).create();
        dialog.show();
    }


}

My app has a login screen that identifies by login field whether user is common or admin, if admin is the stream is redirected to the crud session and if user is common the stream is redirected to a listView where the user can check the items in the list and the only action he can do is to terminate the app. Only when clicking on the back button the message of the alertDialog is shown and instead of the app terminating it goes back to the crud screen, I’ve used finish() and System.exit(0) but none of the 2 closes the app.

Below follows the main class where I receive the login values by the Bundle redirect the flow depending on whether it is ordinary user or admin.

public class principal_Activity extends Activity {

    Button  btnListaClientes,
            btnCadastraEmpresa,
            btnListaEmpresa,
            btnCadastraPromocao,
            btnListaPromocao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        Bundle bundle = getIntent().getExtras();
        String adm = bundle.getString("usuario");
        if (adm.equals("admin")){
            setContentView(R.layout.activity_principal);

        }else{
            Intent i = new Intent(principal_Activity.this, lista_promocao.class);
            startActivity(i);
        }


        btnListaClientes = (Button) findViewById(R.id.btnListaClientes);

        btnCadastraEmpresa = (Button) findViewById(R.id.btnCadastraEmpresa);
        btnListaEmpresa = (Button) findViewById(R.id.btnListarEmpresas);

        btnCadastraPromocao = (Button) findViewById(R.id.btnCadastraPromocao);
        btnListaPromocao = (Button) findViewById(R.id.btnListaPromocao);



    }

    public void listaCliente(View view){
        Intent intent = new Intent(principal_Activity.this, listaClientes.class);
        startActivity(intent);
    }

    public  void cadEmpresa (View view){
        Intent intent = new Intent(principal_Activity.this, cadastra_empresa.class);
        startActivity(intent);

    }

    public  void listEmpresa(View view){
        Intent intent = new Intent(principal_Activity.this, listaEmpresas.class);
        startActivity(intent);
    }

    public  void cadPromocao (View view){
        Intent intent = new Intent(principal_Activity.this, cadastra_promocao.class);
        startActivity(intent);

    }

    public  void listPromocao(View view){
        Intent intent = new Intent(principal_Activity.this, lista_promocao.class);
        startActivity(intent);
    }


    public void mensagem(String titulo,String texto){
        AlertDialog.Builder dialogo= new AlertDialog.Builder(principal_Activity.this);
        dialogo.setTitle(titulo);
        dialogo.setMessage(texto);
        dialogo.setNeutralButton("Confirma",null);
        dialogo.show();
    }

}
  • Guy has long that I do not touch with Android, but try to kill the application I remember is a bad practice. I should go back to the login screen, now I just don’t have the recipe to provide you with how to do this, have to do a search regarding the order of activities and how to terminate or jump to a certain sequence Activity. I hope I didn’t say anything stupid.

1 answer

1

Call the back pressed method and finish the activity with finish(). If you’re in a fragment, call getActivity().finish()

It is very important to keep in mind that when starting a new activity in your app, always finish the previous activity... With this, whenever the user presses the back pressed the application will be terminated...

  • 1

    It worked out old, thank you very much, it was really the cycle of the other activitys that were still running, THANK YOU!!!!

  • You are welcome.... I am happy to help..... if you can mark my answer as the best one found. then I gain reputation.....

Browser other questions tagged

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