简体   繁体   中英

Remove activity from stack, create new instance on Top

My activity flow is like this

A -> B -> C -> B

When I launch B from C, the back stack should be re-ordered to:

A -> C -> B

I'm using FLAG_ACTIVITY_REORDER_TO_FRONT but that re-uses instance from stack, I don't need previous state (I need to call onCreate again.)

Update: Why I need it (Use case)

User navigates from A -> B (changes something here) -> C

Opens up Drawer Navigation on C and launches B. B should be launched with initial state.

Use FLAG_ACTIVITY_REORDER_TO_FRONT and add an extra to the Intent , like this:

Intent intent = new Intent(this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra("reinitialize", true);
startActivity(intent);

Then, in ActivityB do this to recreate the activity when it is brought to the front:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.hasExtra("reinitialize")) {
        // We need to recreate this activity
        Intent launchIntent = new Intent(this, this.getClass());
        startActivity(launchIntent);
        finish();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM