1

Is it possible to clear all previous activities only in this current task? So, my stack of activities looks like this:

A - B - (NEW_TASK) - C - D - E

Is it possible to launch E with some flag:

A - B - (NEW_TASK) - C - D - (SOME_FLAG) - E 

which will remove both activities C and D (all previous activities in current task), so after clicking Back in activity E app will return to the place where the new task was started (Activity B)? After starting activity E, the back stack should be like this:

A - B - E 
diesersamat
  • 667
  • 2
  • 7
  • 18
  • Very good question, please look previous answer about this http://stackoverflow.com/questions/5001882/how-to-clear-specific-activity-from-the-stack-history – W4R10CK Jan 18 '17 at 08:30
  • 1
    @W4R10CK yes, currently I'm using exactly same solution. But this is not very good workaround. If you have a lot of activities between E and B, and they are already destroyed, they will be started again just to pass activity result further – diesersamat Jan 18 '17 at 08:36
  • I'm trying to figure this out now, if successful will update. – W4R10CK Jan 18 '17 at 08:37

3 Answers3

0

you can clear the Activity by calling finish(). override the onBackPressed() and use intent to go to B Activity from E .

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Saneesh
  • 1,796
  • 16
  • 23
0

FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

oldcode
  • 1,669
  • 3
  • 22
  • 41
0

You have so many ways to achieve your usecase

Possible way1: you can use startActivityForResult method to start the activity from C, D and based on result E you can manage to finish the activities C,D on onActivityResult()

Possible way 2: If you don't want to keep the Activities on the backstack, You can finish that activities C, D after they started another

Possible way 3: You can use Intent Flags to manipulate backstack

Let me know if you need further help

For your info You can use finishAffinity() task in Activity to finish all your previous activities. it will Finish the activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch. Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so

Shaahul
  • 538
  • 4
  • 11