How to clear a stack of tasks and leave only new activity?

I want to clear the backstack when I start a new intent.

The exact case of my problem is as follows: I have actions like this A> B> C> D> E "A" is the registration, and "B" is the registration confirmation. I want that when user β€œB” starts, that he can no longer return to activity β€œA”, also when the user reaches activity β€œC”, both operations β€œA” and β€œB” will be useless, I do not need the user returns to him more.

Also, when the user proceeds to action β€œD” with β€œC”, he can return to β€œC”, but if he moves further to β€œE”, he will not be able to return to any of the previous β€œC”, or β€œD” "

After a lot of research, the best I got was to use the flag: FLAG_ACTIVITY_NO_HISTORY , but I have two problems for my case:

  • Under action "A", if the user goes from my application (for example, to the main screen), he destroys this action and, therefore, all the information that the user enters is lost!
  • Activity "C" I want to save it in history (backstack) if the user goes to activity "D", but when the user goes to "E", I want to delete it.

I can’t find a solution to this problem so far, I think it would be easier if there was something to clear the back of the current task!

My minimum SDK support is 5.

Thanks in advance.

+6
source share
1 answer

If you use API level 11 or higher, you can simply add FLAG_ACTIVITY_CLEAR_TASK as you wish, which will clear all other actions in your task.

Otherwise, it may help: Clearing the full stack of Android actions on old SDKs (which lack FLAG_ACTIVITY_CLEAR_TASK)

Another approach would be to complete actions that you no longer want the user to return to. For example, if you call ActivityA.finish () before going to B, the action will be removed from the task.

When you start action D, you can start it with startActivityForResult () with C, and when you close D, send the result to C and then finish the action C before going to E.

+8
source

All Articles