1

I'm trying to figure out how to implement an interesting concept.

Suppose there's a bar at the bottom of my application with 3 buttons. We'll call them A, B, C. I want you to think of these as 3 areas of the app, each with their own Activity stack.

Suppose I press B, I am first taken to the main B screen. Suppose this screen has some functionality within it to take me to another screen, say the details of a particular item on a list.

Now, while on the details screen, I press button C at the bottom and I'm taken to the main C screen.

If I press B (or press the back button), I want the user to be taken to the last screen in the B Activity stack, which is the details screen, and not the main B screen.

Is this possible? If I understand things correctly, I should have 3 tasks, 1 for A, 1 for B, and 1 for C, yes? What else would I have to do?

Andrew
  • 20,756
  • 32
  • 99
  • 177
  • Are you sure this is a good idea? You're going to severely confuse users when you break the normal task stack. It's unusual to show navigations within a tab (or tab equivalent), and normal to pop up a detail view outside the tab controller, which sidesteps this whole issue (the user has to go back from B-> Details View before they can click on C). – Yoni Samlan Apr 13 '11 at 18:44
  • To be honest, it's someone else's proposition. I'm totally against it. The goal, at this point, is to do it, let them play with it and realize why it's a bad idea. – Andrew Apr 13 '11 at 18:49

2 Answers2

0

Actually, the scenario you described only consists ONE activity stack. How many activity stacks will exist is in fact controlled by Android OS, we can only do our best under the rules.

According to the description, the navigation history is B_main -> B_details -> C_main, after BACK is B_main -> B_details. If the three buttons are implemented in all these Activitys (which can be done by customizing some button panel and include that in the layout files), there is indeed only one stack.

shihpeng
  • 5,283
  • 6
  • 37
  • 63
  • Suppose the user hits the B button and reaches the B main, hits an item in a list, reaches B details, hits the C button and reaches C main, hits an item in the list and reaches C details, then hits the B button... The user would need to be at B details because B details was the last screen they were on when they left the "B area" of the application. – Andrew Apr 13 '11 at 19:04
  • The stack simply changes to B_main -> B_details -> C_main -> C_details, nothing else. And I believe any scenario of your Activities can be done by setting Intent flag properly. If you insist to create a new activity stack after pressing C, that will make the communication of the two stacks too complicated. It would be better to design your app navigation carefully according to the conventions. – shihpeng Apr 13 '11 at 19:20
  • B_main-> B_details -> C_main -> C_details. Now click back and forth on the B and C buttons. B_details -> C_details -> B_details -> C_details -> ... – Andrew Apr 13 '11 at 19:29
0

Of course that's possible and simple to implement. But you must read this to understand how to implement that.

You have 3 root activities: A_main, B_main, C_main. Each activity should start a new task. To do that u need:

  1. In your manifest declare different affinities for them:

    activity android:name=".A_main" android:taskAffinity=".A"

    activity android:name=".B_main" android:taskAffinity=".B"/>

  2. To start (switch) task start new intent using flags (you can use ApplicationContext):

    private static final int ROOT_INTENT_FLAG = 
        Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
    

To provide an ui opportunity to switch between tasks you can use e.g. android menu. If you want to clear task - just add flag Intent.FLAG_ACTIVITY_CLEAR_TOP

And...you need some LauncherActivity which will be launched when your application starts. This activity should restore last visited task and finish itself. To restore - just use intent with flags from above (item 2).

Flavio
  • 6,205
  • 4
  • 30
  • 28
  • Is there a way to destroy all tasks? Or will using FLAG_ACTIVITY_CLEAR_TOP clear all tasks? – Andrew Apr 14 '11 at 15:31
  • No, you can't destroy them. But you can move them to background using method of activity moveTaskToBack(). Or...if you want to destroy them very much you should finish all the activities in all tasks of yours. – Flavio Apr 14 '11 at 15:42
  • Hmm... my problem, then, is clicking my "log out" button needs to destroy all tasks, so the next user to log in gets fresh stacks. – Andrew Apr 14 '11 at 15:59
  • When new user starts tasks firstly (he has just log in) - add flag CLEAR_TOP. If old user re-login you have an ability not to clear tasks and restore stack history (if you want). – Flavio Apr 14 '11 at 16:52