1

Imagine this activity history stack:

A > B > C > D > E

scenario 1:

If the user is on E then on tapping the back button it should navigate to D > C > B > A.

scenario 2:

If the user is on E then on tapping a custom button "Show B", then it should clear E > D > C. Which is similar to Finish().

Like X > Y if we set finish on Y the X will be displayed. Similar If I tape Show B on E then E > D > C should be cleared from the stack.

I need to achieve both scenarios.

(Edited ^^^^ with scenarios)

If the user is on E activity and wants to move B. If B is in history stack can we clear C > D > E so that user can navigate to B without startActivity(B). and A should be in history.

If an activity is available in the stack then it should load from history if not startActivity(B).

If I use FLAG_ACTIVITY_CLEAR_TOP/FLAG_ACTIVITY_NEW_TASK, it will clear full history and start's new activity.

I want to clear partial history.

Will it be possible to achieve? If so, how to do it please?

Bullet
  • 87
  • 1
  • 11
  • You can pass flags when starting activity *(I.e. `addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)` if that's your case)*. – Jeel Vankhede Mar 18 '19 at 05:54
  • Read the Android documentation on activity backstack and read this link https://stackoverflow.com/questions/14142936/how-to-control-android-back-stack https://developer.android.com/guide/components/activities/tasks-and-back-stack – raj kavadia Mar 18 '19 at 05:55
  • Also, look at newer available methods and tools for app navigation. You can start including these into your project; https://developer.android.com/guide/navigation/navigation-getting-started https://developer.android.com/guide/navigation https://www.youtube.com/watch?v=Y0Cs2MQxyIs https://www.youtube.com/watch?v=8GCXtCjtg40 – MD Naseem Ashraf Mar 18 '19 at 05:59
  • @rajkavadia updated my question. Naseem – Bullet Mar 18 '19 at 06:37

2 Answers2

1

This is all pretty standard. Don't use any special launch modes. Normally, pressing BACK will just finish the current Activity and drop you back into the previous one.

For this case:

If the user is on E then on tapping a custom button "Show B", then it should clear E > D > C. Which is similar to Finish().

In E, to go back to the existing instance of B, do this:

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

This will finish E, D and C and return to the existing instance of B.

The flag FLAG_ACTIVITY_CLEAR_TOP tells Android to clear all activities between the current Activity and the target Activity. If you don't specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will also be finished and a new instance will be created. If you do specify FLAG_ACTIVITY_SINGLE_TOP then the existing instance of the target Activity will NOT be finished and a new instance will NOT be created.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
-1

You can achieve this using

android:launchMode="singleTask"

in your manifest file. For more refer this documentation https://android.jlelse.eu/android-activity-launch-mode-e0df1aa72242

  • Please don't suggest using the special launch modes `singleTask` or `singleInstance`. These create more problems than they solve, the side-effects are not well documented or undertstood, and they are not necessary in 99% of the situations. This is bad advice. – David Wasser Mar 18 '19 at 14:26