Reputation: 861
I have 3 Activities: A, B, and C, which represent 3 pages of a web form. A is the parent activity of B, and B is the parent activity of C. I have properly implemented the Up Navigation by using android:launchMode="singleTop"
. When I have information is A, information in B, and information in C, I click back when I'm in Activity C, and the information from Activity B shows up. Then I click back again, and the information from Activity a shows up.
Then problem occurs when I go from Activity A to Activity B. Naturally the information I saved on the 2nd page I would want to be there, but it recreates activity B and all of the information is lost. How do I make Activity B resume and not let the activity get destroyed. I read in the docs that when using Up Navigation the current activity is destroyed and the activity from the back stack is resumed. I don't know how to implement this.
Any help is greatly appreciated.
EDIT: A more visual representation of what I want. This is from the docs:
As seen in the picture, when back is pressed while the current Activity is Activity 3, Activity 3 gets destroyed. I don't want this to happen. Can Activity 3 be saved so it can be resumed later?
Upvotes: 1
Views: 499
Reputation: 10859
your solution from the docs
Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
To answer your overall question NO
you go from A
to B
; then back from B
to A
and you want B
to be preserved? what is the probability of comming back to B
to continue your task- so it will be killed and restarted.
To offer some kind of Solution, use ActivityGroup
which is deprecated or (im not really sure if this will work)-change launchmode="singleTop"
to singleInstance
and override its onBackPressed()
to start a new Activity calling the Activity that sparked it-you can get the Activity that sparked it by getIntent()
.. The logic here is singleInstance resides in a different Task and does not allow other Activities in it, so it terms of killing it, it would not die, when a new Activity is sparked,and since your receiving Activity has its lauchmode and its the root you do not have a problem there. the overriding of the onBackPressed is to prevent the normal flow..
Hope it helps this time but overall NO
Upvotes: 1