Search code examples
androidback-buttonback-button-controlclose-application

how to close android app completely


I created an android application with a logout option in onCreateOptionsMenu. The Logout works perfectly but when I press the back button again it takes me to the previous activity, and when it gets a null value it redirects to login screen. Now my login screen appears again and again. I want to close the app completely when I press the back button but don't know how.

Here is my code to handle the back button press in the login screen:

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("BidNEMO")
        .setMessage("Are you sure you want to exit?")
        .setNegativeButton(android.R.string.no, null)
        .setPositiveButton(android.R.string.yes, new OnClickListener() {
         public void onClick(DialogInterface arg0, int arg1) {
         Main.super.onBackPressed();
         finish();
         }
         }).create().show();
     }

please help guyss..


Solution

  • To Quit Application on Button click use this code :

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    

    Try it..

    To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);