1

I'm making an app that has a MainActivity (A) which is a navigation activity. That activity can launch an activity (B) and from there I can navigate to activity (C). Then when I navigate to activity (D) I want to finish all activities before it except MainActivity (A). So essentially if I go back from activity (A) i'll go to the same instance of activity (A).

So essentially A>B>C and then when (D) is launched the stack is A>D.

Hooman
  • 41
  • 3

2 Answers2

1

Two options:

1) Use MainActivity as a "dispatcher". When C wants to launch D, it should do it like this:

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("launchD", true);
startActivity(intent);

This code will clear all activities on top of MainActivity, and causeonNewIntent() to be called on MainActivity.

In MainActivity, override onNewIntent():

@Override
protected void onNewIntent(Intent intent) {
    if (intent.hasExtra("launchD")) {
        // Need to launch Activity D
        Intent launchIntent = new Intent(this, D.class);
        startActivity(launchIntent);
    }
}

MainActivity will now launch D after the stack is cleared. Pressing BACK in D will return to MainActivity.

2) Launch D from C as usual:

Intent intent = new Intent(this, D.class);
startActivity(intent);

At this point, the stack is A->B->C->D

In D, override onBackPressed():

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
}

In this case, pressing BACK while in D will cause D, C and B to be removed from the stack and the user will return to MainActivity.

Note: Using the combination of flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP ensures that the existing instance of MainActivity will be reused. Android won't create a new instance.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

Use the setFlags method to give this Intent the FLAG_ACTIVITY_CLEAR_TOP.

This will check your stack to see if an instance of MainActivity already exists, and if it does it'll bring that Activity to the front and clear all the Activities above it instead of restarting MainActivity and putting it on top of the stack.

Other way is manually calling finish() in every activity.Like :

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
Ratul Bin Tazul
  • 2,121
  • 1
  • 15
  • 23
  • What if activity A isn't called MainActivity. Also for you second solution what if I don't want to close the intermediary activities until I launch activity D – Hooman Nov 12 '16 at 21:21
  • You should not just hand pick several activity you want to be destroyed.Anyway try creating 'Activity' Object then calling the finish() method from another activity.Like this [link](http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity) – Ratul Bin Tazul Nov 12 '16 at 21:46