Search code examples
androidcallstackobject-lifetime

Remove just last instance of an activity from stack


I have an application with 2 activities A and B, A is the main activity and B is called by A but also can be called from B itself.

So my stack will look like

A,B0,B1,B2,B3

In certain cases I want to be able to remove only last instance of B, so [android:noHistory="true"] won't work.

A,B0,B1,B2,B3 => A,B0,B1,B2,B4

How can I accomplish this?


Solution

  • The below lines will finish the activity in which you are working and will start the same activity:

     finish();
     startActivity(getIntent());