Reputation: 43
I have 5 Activities,
A - Base Activity
B,C,D - Normal Activities
E - Final Activity
A-B-C-D-E
I navigate from A to B to C to D, D takes me to E. I want behaviour : if I press back button on D it should take me to C , back button from C should take me to B down to A. But if I have moved to Activity E from D, I want the back button from E it should take me to A skipping B,C and D.
Upvotes: 3
Views: 496
Reputation: 95578
In E
:
@Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
This will go back to A
, finishing B
, C
, D
and E
.
Using FLAG_ACTIVITY_SINGLE_TOP
will ensure that you return to the existing instance of A
. If you want to also finish the existing instance of A
and create a new one to return to, simply remove FLAG_ACTIVITY_SINGLE_TOP
.
Upvotes: 4
Reputation: 1001
you can "group" you activities, by using affinity
.
So in Manifest file add android:taskAffinity="activity.normal"
to activities B,C,D,E. And now you'll be able to finish all this group in any of this activities by calling finishAffinity()
(instead of usual finish()
) in your case you can just add to E activity:
@Override
public void onBackPressed() {
finishAffinity();
super.onBackPressed();
}
Upvotes: 0
Reputation: 61
You can use a static variable to track which activity you're coming from. Then override onBackPressed to check that variable and move to the appropriate activity. Something like:
public static Boolean skipActivities = false;
Then when you start activity E from D set it to true. For your activity E:
@Override
public void onBackPressed() {
if (skipActivities){
//start activity A, skipActivities should be reset to false also
} else {
super.onBackPressed();
}
}
Upvotes: 1