6

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

The exact case of my problem is as following: I have activities like this A > B > C > D > E "A" is a registration activity and "B" is a confirm registration activity. I want that when user start activity "B" that he can't back to activity "A" anymore, also when user reach activity "C", both activity "A" and "B" would be useless, I don't need user back to them anymore.

Also when user moves to activity "D" from "C", he can get back to "C" but if he moved further to "E" activity, he can't get back to any of the previous "C" or "D" activities.

After a lot of research, the best I got is to use flag: FLAG_ACTIVITY_NO_HISTORY, But it has two problems for my case:

  1. At activity "A" if user navigate away from my application (for example to home screen), it destroy that activity and hence all information that user entered is lost!
  2. Activity "C" I want keep it in history (backstack) if while user navigate to "D" activity but when user move to "E" activity I want remove it.

I can't find a solution for this problem till now, I think it would be easier if there something to clear the backstack of current task!

My minimum SDK support is 5.

Thanks in advance.

Elkfrawy
  • 353
  • 2
  • 10
  • 2
    Possible duplicate: http://stackoverflow.com/questions/5794506/android-clear-the-back-stack Also worth looking at: http://developer.android.com/guide/components/tasks-and-back-stack.html#Clearing – jonvuri Oct 24 '12 at 15:59
  • Also similar to http://stackoverflow.com/questions/7121700/remove-all-activities-from-back-stack-while-calling-a-new-activity – speakingcode Oct 24 '12 at 16:01

1 Answers1

8

If you are using API level 11 or higher you can simply add the FLAG_ACTIVITY_CLEAR_TASK on your intent, which will clear all the other activities in your task.

Otherwise, this might help: Clearing the full Android activity stack on older SDKs (that lack FLAG_ACTIVITY_CLEAR_TASK)

Another approach, would be for you to finish the activities you no longer want the user to be able to return to. For instance, if you call ActivityA.finish() before moving to B, the activity will be removed from the task.

When you start the activity D, you can start it using startActivityForResult() from C, and when you close D, send the result to C, and then finish activity C before navigating to E.

Community
  • 1
  • 1
kleinsenberg
  • 1,323
  • 11
  • 15
  • Thanks a lot for your reply, I applied the first approach by calling "finish()" function on the activity before starting the other one, and it works great. But for the second part, activity "C" starts activity "D" and activity "E" is opened by activity "D" not "C". So can I pass a reference to activity "C" to activity "D" so I can destroy it before starting activity "E"? – Elkfrawy Oct 24 '12 at 16:47
  • 1
    You shouldn't pass a reference. What I was trying to say is that you could start activity "D" from "C" using the startActivityForResult() method. When you decide to go to "E", set the result in activity "D", and then finish it, which will send you to "C", where you verify the result, and redirect the user to "E", also finishing activity "C". It is a bit complicated to explain, I hope you get it. – kleinsenberg Oct 24 '12 at 17:10
  • Ya, this was clear, I applied it and it worked as I want. Thanks a lot for your help – Elkfrawy Oct 24 '12 at 17:43