I have not tried this myself, but I think the best option would be to refactor your application to use the Fragment stack within the same Activity (since then you can more easily control the still frames using the provided addToBackStack() and popBackStack() ) . Basically this involves moving most of the code in your activity into a fragment, and then adding the Backstack manipulation code to the Activity). You can look at the FragmentBreadCrumbs code (with API 11+) or the code for HanselAndGretel (for use with the compatibility library) to see how this can be implemented.
However, if you want to continue to use your current multitasking approach, the following code that I have provided to illustrate how you can do this.
First, add a few inner classes for the alias of your current action and put these classes in a sequence list (note also the simplified getSequencedActivityIntent() method that I wrote, you can add more complex logic if you need to - maybe use a HashMap to bind each class in sequence with arbitrary tag value?):
public class MyActivity extends Activity { public static class A extends MyActivity {} public static class B extends MyActivity {} public static class C extends MyActivity {} public static class D extends MyActivity {} public static class E extends MyActivity {} public static class F extends MyActivity {} public static class G extends MyActivity {} public static class H extends MyActivity {} public static class I extends MyActivity {} public static class J extends MyActivity {} private final static List<Class<?>> SEQUENCE = Arrays.asList(new Class<?>[] { A.class, B.class, C.class, D.class, E.class, F.class, G.class, H.class, I.class, J.class, }); private Intent getSequencedActivityIntent(int step) { final int current = SEQUENCE.indexOf(this.getClass()); if (current == -1) new Intent(this, SEQUENCE.get(0)); final int target = current + step; if (target < 0 || target > SEQUENCE.size() - 1) return null; return new Intent(this, SEQUENCE.get(target)); }
Remember to also add your entries to your AndroidManifest.xml file ( singleTop is optional - this will cause the Activity instance on the stack to be created again when you return to the foreground):
<activity android:name=".MyActivity$A" android:launchMode="singleTop" /> <activity android:name=".MyActivity$B" android:launchMode="singleTop" /> <activity android:name=".MyActivity$C" android:launchMode="singleTop" /> <activity android:name=".MyActivity$D" android:launchMode="singleTop" /> <activity android:name=".MyActivity$E" android:launchMode="singleTop" /> <activity android:name=".MyActivity$F" android:launchMode="singleTop" /> <activity android:name=".MyActivity$G" android:launchMode="singleTop" /> <activity android:name=".MyActivity$H" android:launchMode="singleTop" /> <activity android:name=".MyActivity$I" android:launchMode="singleTop" /> <activity android:name=".MyActivity$J" android:launchMode="singleTop" />
Now that you need to launch a new “top” instance of your activity, you can do something like:
final Intent intent = getSequencedActivityIntent(+1); if (intent == null) return; intent.putExtra("dataset", dataSet); startActivity(intent);
And when you need to return to one of the instances in the backstack, you can do:
final Intent intent = getSequencedActivityIntent(- stepBack); if (intent == null) return; intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);