1

Example: I have an android app with 3 activities that has the following behaviors:

A (Home) -> B -> C

Activity A launches Activity B
Activity B launches Activity C

When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B

What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.

So I don't want to have:

 1) A
 2) A-B
 3) A-B-C
 4) A-B-C-A

What I would like is:

 1) A
 2) A-B
 3) A-B-C
 4) A

How can I do this?

Mark Lapasa
  • 1,644
  • 4
  • 19
  • 37

3 Answers3

1

Check out the intent stack machinery!

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

Bondax
  • 3,143
  • 28
  • 35
  • You should definitely read and understand this document. If you're in a rush, what you're looking for is `FLAG_ACTIVITY_CLEAR_TOP`. – sastraxi Mar 27 '12 at 17:34
  • I spent an hour reading that doc and there is nothing in there about the use case I defined. The closest thing was "singleTask". I would like to have Activity A configured to use singleTask and when they get to Activity C and hit that "My Root Activity" button, it would do something like "finishTask()" or something to that effect so that child B and C get removed. – Mark Lapasa Mar 27 '12 at 17:41
0

EDIT: You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.

"singleTaks"

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

"singleInstance"

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

Check out this link

bughi
  • 1,838
  • 1
  • 13
  • 24
  • Nice but if I am on Activity C, i will have no way to go back to Activity B -> "When user is on Activity C and they hit the Back button, it takes them Activity B" – Mark Lapasa Mar 27 '12 at 17:32
  • oh sorry, thought you no longer needed activity B – bughi Mar 27 '12 at 17:37
0

I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.

This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.

Mark Lapasa
  • 1,644
  • 4
  • 19
  • 37