2

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.

Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • Could you provide a visual example of exactly what you want the backstack behavior to be ? – Yash Sampat Feb 23 '15 at 11:29
  • @ZygoteInit I tried to give you a visual example. I hope this is sufficient. What I want is to remember the backstack so when I return to an item in my navigation drawer I can continue where I was the last time I clicked that item – Kevin van Mierlo Feb 23 '15 at 11:36
  • You definitely do NOT want to use multiple tasks to solve this problem. It will be a usability nightmare. Each task ends up in the list of recent tasks and you have to be very careful about how to allow the user to return to a specific task. This is almost impossible to get right. Don't do it. – David Wasser Feb 23 '15 at 17:47

1 Answers1

1

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.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I already do this, but thanks. What I want is to "save"(sort of) the backstack when you press another navigation drawer item, so when you return you can continue with the activity where you were earlier. Do you understand? I find it hard to explain haha :P – Kevin van Mierlo Feb 23 '15 at 12:52
  • I understood, and as I said, it will be very difficult to write code that does that reliably :/ – Yash Sampat Feb 23 '15 at 12:55
  • Ahh okay, sorry. Hoped this would be possible since Android already handles this itself, but too bad. Thanks for helping though! – Kevin van Mierlo Feb 23 '15 at 12:59
  • 1
    What I understood from the link in my question is that if you start a new Activity with the flag NEW_TASK and that activity has a different affinity it creates a new task with a new backstack. Did I misunderstand this? – Kevin van Mierlo Feb 23 '15 at 13:03
  • yes that`s right, but what you want is for the old backstack (A1-A2-A3) to be joined with a new one to get A1-A2-A3-A4-A5. This is how I understood your question, and I don't think FLAG_NEW_TASK achieves that – Yash Sampat Feb 23 '15 at 13:06
  • 2
    Not exactly. I want to keep the old backstack in memory while creating a new task (or in any other way) so first I have A1-A2-A3 then I start a new task and have A1-A4. When I go back to the other item I want to switch tasks and have the A1-A2-A3 backstack again. So for every item in my navigation drawer I have a different task and a different backstack. – Kevin van Mierlo Feb 23 '15 at 13:10
  • An application usually consists of a single task, so a requirement like `for every item in my navigation drawer I have a different task and a different backstack` would not be feasible I believe ... besides, it does not sound like good navigation practice to me, but I will look into it in my spare time :) – Yash Sampat Feb 23 '15 at 13:15
  • Yeah you are probably right about that, but I thought this might be the way to do it. I didn't know any other method to achieve this. But if this is a really bad way to do it, I'll just stick with what I have right now :P – Kevin van Mierlo Feb 23 '15 at 13:18