I have application with activity A without any custom launch modes and activity B which is started from application context with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_SINGLE_TOP flags - it need's to be in new task because it's started from specific place where I don't have activity context available but it's not important for this particular issue. Now when activity B is started situation is as below:
Stack 1 has activity A
Stack 2 has activity B
User is now on activity B (stack 1 in background, stack 2 in foreground) and press 'home' - in this moment activity A is destroyed and Stack 1 is removed! After returning to the app user lands on activity B (properly) but after finishing it user leaves the app instead of returning to activity A.
I try to understand why "Stack 1" Is being removed when app is minimized - I don't believe it's proper behavior but I can't find any reasons why it's working like this. Do you have any tips?
Related
Actually the scenario is a bit more complex than described it the title.
The situation is the following:
Activity A starts Activity B.
Activity A must not be destroyed when I start Activity B because I
need the user to be able to navigate back to A.
When the user presses the HOME button the user opens the Recent Apps
window and switches from my app to another app. At this stage both A
and B are STOPPED.
When the user user opens the Recent Apps window and switches back for
the other app to my app: Activity B is RESTARTED (activity A is not
restarted yet)
Now on Activity B there is a button to close the entire app, closing
both B and A, and it does close both activities using this approach:
https://stackoverflow.com/a/11509279/1815311
THE PROBLEM IS THAT WHEN ACTIVITY B TRIES TO CLOSE BOTH B AND A, IN
THE DESCRIBED SCENARIO, ACTIVITY A SOMETIMES IS NULL !!
How do I cope with such a scenario?
1 solution is - before finishing the activity B, store some variable with value 1 using sharedpreferences, finish activity B but not A. system will resume activity A. override onResume() function in activity A and get the variable from shared preferences, if it states 1 then store 0 there, and finish() activity A.
2 solution is overriding onresultactivity - see here
How to kill an application with all its activities?
I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.
Activity A - starting activity with map
Activity B - user navigates to Activity B (a list view) from Activity A.
The user then selects the map icon to navigate to Activity A again.
So if you can image it, my activity stack is now:
Activity A
Activity B
Activity A
So if I press the back button the device it will take forever as it scrolls through activities.
Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.
Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.
One simple solution is to kill the Activities as soon as they leave the foreground.
You could do that by calling finish() inside onPause().
You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:
D
C
B
A
D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)
I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest
I have to be honest this is a question which is at present academic but I have searched extensively and cannot find anyone who describes the answer. It even caused me to have an awful night's sleep last night as it manifested itself in me dreaming about continually trying to find a white puppy but I was always unable to find the puppy. So you see, this is both technically and mentally a pretty serious issue.
So,
Let's say I have a Task stack of 3 activities, A, B, C. A has launch mode "singleTask", and let's make activities B and C launch mode "standard".
So, Task 1 is
A -> B -> C
Let's say I have a second Task which is composed of just 2 activities, X (the root activity with launch mode "standard") and a 2nd activity which is actually Activity A.
So, Task 2 is
X -> A
**Question :
When activity X invokes activity A (which is defined launch mode "singleTask", what happens to the children of activity A on Task stack 1 (ie B and C) ???
When I invoke activity A, from activity X, do I end up with back stack :
X -> A -> B -> C (ie activity C becomes displayed)
Or
X -> A (ie the previous children of Task A are removed from the front of the back stack)
Also, regardless of the answer above, if I were to "BACK" key all the way back through the stack for Task 2, what happens to the original Task 1's stack. Has it now become gobbled up by Task 2 and so no longer exists ?
I want to go for a big run today so the thought of another night searching for the white puppy is rather depressing.
Any response or a link would help.
The example given on the Android Dev section covers how ancestor activities of the "singleTask" activity are "merged" into the back stack of the Task which invokes the "singleTask" activity but unfortunately it does not describe what would happen to the children of the "singleTask" activity.
Many thanks in advance ladies and gents.
Paul.
**Question : When activity X invokes activity A (which is defined launch mode "singleTask", what happens to the children of activity A on Task stack 1 (ie B and C) ??? When I invoke activity A, from activity X, do I end up with back stack : X -> A -> B -> C (ie activity C becomes displayed)
Or X -> A (ie the previous children of Task A are removed from the
front of the back stack)
Neither would happen. Tasks are not mixed up, a activity may move to a task having same affinity.
So there would be two tasks now, Task 1 would have only activity A, and Task 2 would have only activity X. Activities B and C would be cleared from the Task 1.
Although, as discussed at the following links
1. Android: bug in launchMode=“singleTask”? -> activity stack not preserved,
2. Making activity singleTask destroys stack after returning? and
3. Issue 11160: Behaviour of launchMode=“singleTask” not as described,
if there is a bug then the Task 1 should has just brought to foreground with activities B and C.
if I were to "BACK" key all the way back through the stack for Task 2, what happens to the original Task 1's stack. Has it now become gobbled up by Task 2 and so no longer exists ?
So now the "Back" key behavior would be according to the new tasks, and as stated at Tasks and Back Stack
When the user presses the BACK button, the current activity is popped
from the top of the stack (the activity is destroyed) and the previous
activity resumes (the previous state of its UI is restored).
Activities in the stack are never rearranged, only pushed and popped
from the stack—pushed onto the stack when started by the current
activity and popped off when the user leaves it using the BACK button.
As such, the back stack operates as a "last in, first out" object
structure.
Hope, it helps.
Suppose my application P started Activity A, and A started Activity B, then the activity stack contained A and B, with B at the top.
After that, I opened other apps by pressing Home button. Suppose after a while, the process of application P is killed by the OS. Then, I press Home button to select application P. At this time, which activity will be brought to the front, Activity A or Activity B?
Thanks.
If the application was killed, then Activity A would be started and brought to the front on application restart. This happens because your manifest specifies that Activity A is launched when the app icon is clicked.
(Of course, if your application wasn't killed, then your original activity stack would reappear - B at the top with A underneath)