Kevin van Mierlo
Kevin van Mierlo

Reputation: 9814

Android create different backstacks (tasks) and return to them

I have read this article: http://developer.android.com/guide/components/tasks-and-back-stack.html

If I read it correctly it says you can create a new task with a new activity while the old task still has a backstack. They are also talking about returning to an old taks. However I did not find how to implement this.

What I want:

I have a navigation drawer. I want to click an item. When back is pressed it goes back to the root. But if you click another item, the backstack is saved in the current task. Then a new backstack is created in this other item. When clicking the first item again I want to go to the top of the backstack, so continuing where you were.

Example:

  1. A1 -> A2 -> A3 (drawer item clicked) (so backstack is (A1, A2, A3))
  2. A1 -> A4 -> A5 (last drawer item clicked)
  3. A3 (With in backstack A1, A2, A3)

Is this possible?

NOTE: I work with activities, it is a large app and activities are easier to maintain.

Upvotes: 2

Views: 833

Answers (1)

Yash Sampat
Yash Sampat

Reputation: 30611

The purpose of the NavigationDrawer is to simplify navigation that would otherwise be complex and tedious to manage, both for the user and programmer. What you should do is, whenever you start a new Activity from the root Activity (A1), you should start it like this:

Intent intent = new Intent(this, A2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent);

This will ensure that A1 is ALWAYS the root of the backstack.

I don't know what specific reasons you have for wanting the behavior you describe, but I would consider what I have described above to be appropriate, graceful handling of the backstack.In any case, it would be extremely challenging to implement the behavior you are asking for.

Upvotes: 1

Related Questions