Android Clear Middle Activities


Android Clear Middle Activities



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.





did you try overriding the onBackPressed in the Activity E to navigate you to A?
– Muhannad Fakhouri
Oct 30 '16 at 18:10




3 Answers
3



In E:


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.


A


B


C


D


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.


FLAG_ACTIVITY_SINGLE_TOP


A


A


FLAG_ACTIVITY_SINGLE_TOP





Thanks. This is exactly i was looking for
– Sahil Gupta
Nov 4 '16 at 12:09



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();
}
}





Using static variable for this is the wrong approach. This is bad architecture. There are simple Intent flags that do exactly what OP wants.
– David Wasser
Oct 31 '16 at 15:31


static


Intent



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:


affinity


android:taskAffinity="activity.normal"


finishAffinity()


finish()


@Override
public void onBackPressed() {
finishAffinity();
super.onBackPressed();
}





Horrible answer. Using taskAffinity for this purpose is completely wrong. Not only that but this feature exists only on API level 16 and higher. If you provide different taskAffinity on different activities in the manifest, you will quickly end up with 2 tasks for your application, which is certainly not what you want, nor what the user expects. Doing this opens a large can of worms that you need to completely understand before doing it. There are simple Intent flags to do exactly what OP wants, and it works on API level 1 and higher.
– David Wasser
Oct 31 '16 at 15:30


taskAffinity


taskAffinity


Intent





Thanks! Didn't know about such flag!
– Boris Safonov
Oct 31 '16 at 15:38






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV