Could someone please explain what FLAG_ACTIVITY_SINGLE_TOP do? The docs say
If set, the activity will not be launched if it is already running at
the top of the history stack.
But that statement seems to be burying a great deal of meaning beneath it. For instance someone online mentioned that the top activity may not be the same as the activity at the top of the task stack. I have no idea what all of that means. Hence my greater question: what are the implications of using FLAG_ACTIVITY_SINGLE_TOP?
It means if the activity is already up and you call it again you wont replace it with a new one and you wont create another one (which sometimes happens and is evident when you hit back and you see the same activity up), instead you will pull that one up.
So say you have 3 activities: A -> b -> c. You are in C and you came to it through A and then B. If you call to A, from C with the SINGLE_TOP filter your stack will look like A, C, B - if you started to hit the back button, you would go to C, then B. I could be wrong but thats how i believe it is. You can also pass in the CLEAR filter with it to erase the back stack and techniquely be started back at A with no stack in back of it, you would back straight out to home. - please correct if im inaccurate.
The following is an answer I read online. It is not a complete answer to the question you are asking, so I am really hoping someone else can add a lot more meat to it.
If an instance of the activity already exists at the top of the
current task, the system routes the intent to that instance through a
call to its onNewIntent() method, rather than creating a new instance
of the activity. The activity can be instantiated multiple times, each
instance can belong to different tasks, and one task can have multiple
instances (but only if the the activity at the top of the back stack
is not an existing instance of the activity).
For example, suppose a task's back stack consists of root activity A
with activities B, C, and D on top (the stack is A-B-C-D; D is on
top). An intent arrives for an activity of type D. If D has the
default "standard" launch mode, a new instance of the class is
launched and the stack becomes A-B-C-D-D. However, if D's launch mode
is "singleTop", the existing instance of D is deliverd the intent
through onNewIntent(), because it's at the top of the stack: the
stack remains A-B-C-D. However, if an intent arrives for an activity
of type B, then a new instance of B is added to the stack, even if its
launch mode is "singleTop".
Source
Related
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 three activities A, B and C.
A is the main activity of my application.
A and C can also be started from the Options Menu, B is started from A.
I would like the following behavior:
application starts with A: back stack is {A}
from A, I navigate to B : back stack is {A,B}
from the options menu, I start C : back stack is {C}
1 and 2 are trivial but I don't succeed in getting 3 to work.
I tried quite a lot of FLAG_ACTIVITY combinations but without success
and I'm getting the impression that this isn't possible.
I'm thinking about creating a DummyRoot activity that is just used to start another activity (actual activity name to start is passed in Intent.getExtras()). I can give this DummyRoot activity the FLAG_ACTIVITY_CLEAR_TOP.
By doing so, I would get
application starts with DummyRoot(A): back stack is {DummyRoot, A}
from A, I navigate to B : back stack is {DummyRoot, A,B}
from the options menu, I start DummyRoot(C) : back stack is {DummyRoot, C}
Do you foresee problems with this approach?
Is this needed in the first place or is it possible to clear the back stack in a more elegant way?
So when you press back whilst in C, you want the application to quit? If so, you will need to use Flags in your manifest and when you start the Activity using the Intent. A combination here will allow you to clear the current Task and then start a fresh one with your new Activity in it. It does not seem a valid UX, but should do the trick. Please read up on the FLAGS in the documentation for more info on what they will actually do.
In intent I can set such flag as FLAG_ACTIVITY_SINGLE_TOP. Can somebody explain me its meaning, cause I just don't get it? =)
The tasks page says for single top:
If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is not an existing instance of the activity).
For example, suppose a task's back
stack consists of root activity A with
activities B, C, and D on top (the
stack is A-B-C-D; D is on top). An
intent arrives for an activity of type
D. If D has the default "standard"
launch mode, a new instance of the
class is launched and the stack
becomes A-B-C-D-D. However, if D's
launch mode is "singleTop", the
existing instance of D is deliverd the
intent through onNewIntent(), because
it's at the top of the stackāthe stack
remains A-B-C-D. However, if an intent
arrives for an activity of type B,
then a new instance of B is added to
the stack, even if its launch mode is
"singleTop".
Javadoc says:
If set, the activity will not be launched if it is already running at the top of
the history stack.
I have an app that I have running a media player and I want to resume the activity from my apps home activity.
I can successfully do this by adding the following flags to the startActivity Call:
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
I am worried that this is not an ideal way to do things since it took me a long time to find it. It made me think that no one uses it very much.
Are there any pitfalls to using this method?
I know that this question is quite older and may you have solved your problem and may be travelled to mars and back in those years.But just to make things clear for the people coming here and looking for an explanation:
According to Official Documentation:
If set, the activity will not be launched if it is already running at the top of the history stack.
Suppose you have BackStack of A-B-C-D and You have launched
another intent Starting Activity A with FLAG_ACTIVITY_CLEAR_TOP
at this point what android will be do is Simply Clear All the
Activities on the Top of Activity A, means now your BackStack Will
Look like-> A (yes that's it because you have cleared all the
activities on top of it).
And In Second Scenario Suppose You have the same BackStack A-B-C-D
and you launched an Intent Starting Activity A with
FLAG_ACTIVITY_SINGLE_TOP, now your BackStack will look like->
A-B-C-D-A (Confused? Don't Worry Next Example Will Clear All Your
Confusions)
Third Scenario, We start with the same BackStack A-B-C-D and We
will launch an Intent Starting Activity D with
FLAG_ACTIVITY_SINGLE_TOP, now our BackStack will look like->
A-B-C-D (Yes the BackStack remains in the Same Order because our
FLAG prohibits the launch Same Activity If it is the Currently on Top
of the Stack and Showing on the User screen.
Last Scenario, We start with our same BackStack A-B-C-D and We will
launch an Intent Starting Activity D but this time with no FLAGS,
now our BackStack will look like-> A-B-C-D-D.
Got it? FLAG_ACTIVITY_SINGLE_TOP is only useful When you are
starting the Same Activity Which is Currently Showing On the Screen
and You want to prohibit the launch new Instance of the Existing Activity.
Just to make sure I understand your question correctly: Say your activity stack is something like A -> B -> C, and now from C you want to clear the stack and get back to A.
If this is what you want, then those intent flags are correct and should work as expected, as per the Android-developer docs.