Question

How can I kill all the Activities of my application?

I tried using this.finish() but it just kills one Activity.

In fact, I would like when the user touches the back button (in only one of the activities), the application to do the same as if he pressed the Home button.

Was it helpful?

Solution

You can set android:noHistory="true" for all your activities at AndroidManifest.xml. Hence they will not place themselves onto history stack and back button will bring you to the home screen.

OTHER TIPS

This has been discussed many times before. Essentially there's no easy way, and there's not supposed to be. Users press Home to quit your app (as you have pointed out).

Override the onBackPressed method, and do what happens when they press the home button. It's not really closing all the activities but what you're asking is that the back press in a certain activity emulates the home button.

@Override
public void onBackPressed() {
    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    startActivity(i);
}

Placing that in an activity will go home when you press back. Note that the original implementation of the onBackPressed method includes a call to super, it's removed on purpose. I know this question is super old, but he's explicitly asking something else

To kill all the activities, use these code on onBackPressed.

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount()>0) {
       getFragmentManager().popBackStackImmediate();            
    } else {
          finishAffinity();
          System.exit(0);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top