Is there a FLAG_ACTIVITY_**** to bring a previous Activity to front and finish all the others between the stack?

StackOverflow https://stackoverflow.com/questions/18041284

  •  23-06-2022
  •  | 
  •  

Question

I have currently some Activities in my stack, let's imagine:

A,B,C,D,E.

I am in Activity E and my "up navigation" should bring Activity A to the front and finish (B,C,D).

I am currently working with:

        case android.R.id.home:
            Intent intent = new Intent(this, ActivityA.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
            finish();
            return true; 

This is nice and Activity A comes back in it's previous state, but (B,C,D) still exist.

Is there a way to correctly finish (B,C,D)?

Note: I am searching to avoid complicated stuff like onActivityResult that would be impossible to deal with in a complicated application structure

Thanks.

Was it helpful?

Solution 2

I think FLAG_ACTIVITY_CLEAR_TOP is what you need:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

Although if you want to reuse the existing instance of Activity A (via onNewIntent()) then you might also need to set FLAG_ACTIVITY_SINGLE_TOP in the intent.

OTHER TIPS

Try with this:

  case android.R.id.home:
        Intent intent = new Intent(this, ActivityA.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return true; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top