Search code examples
androidandroid-activity

Delete old activity instance when starting a new activity


I'm looking to delete/remove an old activity instance when a new instance (of same activity) is created, however I need to maintain all other activities in back-stack (therefore FLAG_ACTIVITY_CLEAR_TOP won't suffice).

E.g. say I have activities A, B & C. I start: A -> B -> C -> B. On the start of the second B activity I need to remove the existing B activity. Therefore I now have: A -> C -> B running...

Any help appreciated.


Solution

  • It seems that deleting the activity is not as easy as I would have imagined therefore not a complete answer but I'm going to go with using FLAG_ACTIVITY_REORDER_TO_FRONT. This will not kill the existing activity but instead move it to top of stack.

    Intent intent = new Intent(ctx, Activity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    

    This will allow for the activity navigation outlined above.

    I'm still interested if someone knows of a means to clear the actual activity.