tags:

views:

107

answers:

1

Hi guys,

I've got the following:

Activities A, B, C, D. A and D can be reached at any time, anywhere from the application.

B and C are reached like this:

A -> B -> C

I have the following use case:

The user has entered C ( A -> B -> C ) then she has gone to D.

When she wants to go to A, I want to transfer her to the already started queue from A - in other words I want her to go to the started C.

Something like this A -> B -> C -> D -> (same) C. But I don't want to lose D from the activity stack. After this, when she presses the "back" button, she is transfered to D again.

Is this possible and correct to do? What is the best practice?

If I haven't explained my situation clear enough, feel free to ask questions.
10x in advance,
Danail

+2  A: 

Unfortunately you can't do this. The documentation says the following:

Note that when a new instance of an Activity is created to handle a new intent, the user can always press the BACK key to return to the previous state (to the previous activity). But when an existing instance of an Activity handles a new intent, the user cannot press the BACK key to return to what that instance was doing before the new intent arrived.

Basically you can't have the same instance of Activity C in two places in the stack at the same time.

However, you could make it appear that this was the case by making Activity C save its transient state to the Application object for your application. This would allow all instances of Activity C to share state and so appear as if they were the same instance.

You'd need to create your own subclass of Application and then read and write to it from Activity C in onResume() and onPause().

Dave Webb
10x! I'll try that.
Danail
actually, just posting this from another answer, there is a way to do this:"You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT"
Danail
@Danail - That doesn't quite do what you wanted. It will change the order to `A -> B -> D -> C` which means pressing back from Activity D will take the user to `B`. (I think so from reading the documentation anyway, I haven't tried it.)
Dave Webb
absolutely right! But still, it saves me from creating a LOT of unnecessary objects, and I think it can help people landing on this question.
Danail