Skip to content Skip to sidebar Skip to footer

I Want To End The Whole Application When User Click Back Button In Android

I want to end the whole application when user click back button in android.Currently It again go to previous opened activity. I also try override onBackPressed() but it doesnt work

Solution 1:

Try this, start your application from a Activity that will act as your Root.

publicclassRootextendsActivity {

     publicvoidonCreate() {
         super.onCreate();
         handleIntent();
      }

      publicvoidonNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent();
      }

     privatevoidhandleIntent() {
         boolean end = getIntent.getBooleanExtra("End");
         if (end) {
            finish();
         } else {
            Intent intent = newIntent(this, MyRealFirstActivity.class); // where this is the class that you want the user to see when they launch your app.startActivity(intent);
         }
      }



      publicvoidonResume() {
        super.onResume();
        finish();
      }
}

Then inside the activity that you want the back button to end the app, do the following:

publicclassLastActivityextendsActivity {

    publicvoidonBackPressed() {
        Intent intent = new Intent(this, Root.class);
        intent.putExtra("End", true);
        intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

This way all the activities that were started in between you launching the app and then hitting the back button, will all be finished() for you. This essentially will clean the slate of the app from start to finish and then exit.

Solution 2:

In this case you need to finish activity, so your current activity will be closed by using finish() method. but you should also write finish() in each and every previous activities, where you call intent(start another activity). Hope it makes clear.

Solution 3:

publicvoidonBackPressed() {

         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         this.finish();
         startActivity(intent);
       }

Solution 4:

Here we have two methods to finish or kill the app on back button pressed.

  1. Using finish() which closes or finishes the current activity on android screen.

    for example : you have 2 activities i.e A & B. You ll go from A activity to B activity using intent, now foreground acitivity is B, you want to go back & kill B activity & goto A acitivity use finish() onclick of backbutton. If your on A activity then it closes the app. see below code.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    finish();
    }
    return super.onKeyDown(keyCode, event);
    }
    
  2. Using android.os.Process.killProcess(android.os.Process.myPid()); which kills the app i.e force close the app & goto the home screen.see below code.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    // Kills & force closes the app 
    android.os.Process.killProcess(android.os.Process.myPid());
    }
    return super.onKeyDown(keyCode, event);
    }
    

Solution 5:

Here's the way I did it in my app: ActivityA is the first one to start; it in turn starts ActivityB. In ActivityB I may encounter an error, in which case I Want to return to activityA, or everything may finish correctly, in which case I want to "finish" the app altogether.

In ActivityA:

publicclassActivityAextendsActivity {
...
    privatefinalstaticintREQUEST_ACT_B=1;
...
    privatevoidstartChild() {
        startActivityForResult(newIntent(this, ActivityB.class), REQUEST_ACT_B;
    }

    @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_TASK && resultCode == ActivityB.RESULT_OK) {
            this.finish();
        }
    }
}

And then in ActivityB:

publicclassActivityBextendsActivity {
...
    publicfinalstaticintRESULT_OK=1;
    publicfinalstaticintRESULT_ERROR=2;
...
    privatevoidfinishWithError() {
        setResult(RESULT_ERROR);
        finish();
    }

    privatevoidfinishSuccessfully() {
        setResult(RESULT_OK);
    }
}

Essentially, ActivityA starts ActivityB and expects a result back. If it receives back result "OK", then it closes itself and the user is none the wiser: the app has finished. If it received result "error", then ActivityA stays open.

Post a Comment for "I Want To End The Whole Application When User Click Back Button In Android"