Amit Yadav
Amit Yadav

Reputation: 35114

Android launch activity on top of particular activity

Say, I have below Activity

Activity A, Activity B, Activity C and Activity D

Currently stack is having below Entry

Activity C
Activity B
Activity A

I have to launch Activity D so that stack become like below,

Activity D
Activity A

What flag I have to set?

Upvotes: 2

Views: 5758

Answers (2)

David Wasser
David Wasser

Reputation: 95636

Consider using A as a dispatcher. When you want to launch D from C and finish C and B in the process, do this in C:

// Launch A (our dispatcher)
Intent intent = new Intent(this, A.class);
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished
//  and setting SINGLE_TOP ensures that a new instance of A will not
//  be created (the existing instance will be reused and onNewIntent() will be called)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Add an extra telling A that it should launch D
intent.putExtra("startD", true);
startActivity(intent);

in A.onNewIntent() do this:

@Override
protected void onNewIntent(Intent intent) {
    if (intent.hasExtra("startD")) {
        // Need to start D from here
        startActivity(new Intent(this, D.class));
    }
}

Upvotes: 3

Nyxar
Nyxar

Reputation: 54

Did you try this ?

<activity name="Activity D"
   android:allowTaskReparenting="true"
   android:taskAffinity="Activity A" >

Upvotes: 0

Related Questions