Example : I have 3 Activities, A,B, and C. from Activity A I open Activity B then From B open Activity C. Then I exit application by code :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
System.exit(0);
I use this code for exit app. But when restart app, back again to recent Activity. My question, How to finish all Activities when exit from app?
You must be back to your main activity first by this code:
Intent home = new Intent(this, mainActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home);
And then you will exit your application from mainActivity like this:
finish();
System.exit(0);
Hope it helps.
Whenever you are calling from one Activity to another activity, try to clear the activites stack by using the following flag:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Hope it helps.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);