Arya Susastra
Arya Susastra

Reputation: 37

How to finish all activity when exit from child activity

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?

Upvotes: 1

Views: 5970

Answers (3)

Dewa Yudie
Dewa Yudie

Reputation: 117

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.

Upvotes: -2

moDev
moDev

Reputation: 5258

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.

Upvotes: 2

Hesham Saeed
Hesham Saeed

Reputation: 5378

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);

Upvotes: 1

Related Questions