0

For example,

I have activity A, B, C, D

A call B

Intent intent = new Intent(A,B.class);

startActivity(intent);

Then, B call C

Intent intent = new Intent(B,C.class);

startActivity(intent);

After that, C call D

Intent intent = new Intent(C,D.class);

startActivity(intent);

In Activity D, I call finish(). It will return back to Activity C.

My question is how can I clear Activity A, B, C before calling finish() so that the app quit like normal.

Don't suggest call finish() on every startactivity because the app can press back to previous activity to continue.

Alan Lai
  • 1,296
  • 2
  • 12
  • 16
  • you mean you want to clear all activity stack before calling some activity? – Biraj Zalavadia Sep 05 '13 at 06:56
  • check my answer there http://stackoverflow.com/questions/18570838/resuming-an-activity-without-finishing-the-others/18570914#18570914 – Lia Pronina Sep 05 '13 at 06:56
  • @BirajZalavadia now the stack have activity A, B, C and D and now I call `finish()` in activity D and the app should quit instead of back to activity C – Alan Lai Sep 06 '13 at 07:25

5 Answers5

2

This should work definitely...

Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);

and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
 //ur previous code here
}

Have fun if any problem u can ask

1
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK

which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Activity A -> Activity B and then you want to get back to A from B, but the extra flags shouldn't affect your case above).

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
0

Try adding FLAG_ACTIVITY_NEW_TASK.

So your code would be:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Kaidul
  • 15,409
  • 15
  • 81
  • 150
  • now the stack have activity A, B, C and D and now I call finish() in activity D and the app should quit instead of back to activity C. `FLAG_ACTIVITY_NEW_TASK` require api 11 – Alan Lai Sep 06 '13 at 07:28
0

I am using the following in my application. Hope it will help.

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);

and in Activity A add the following:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    if( getIntent().getBooleanExtra("exitme", false)){
        finish();
        return;
    }
}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

try with Intent.FLAG_ACTIVITY_CLEAR_TOP

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

see here http://developer.android.com/reference/android/content/Intent.html

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • now the stack have activity A, B, C and D and now I call finish() in activity D and the app should quit instead of back to activity C. `FLAG_ACTIVITY_CLEAR_TOP` is useless here and `FLAG_ACTIVITY_NEW_TASK` require api 11 – Alan Lai Sep 06 '13 at 07:26