Android launchMode
Encircle the wood, was born
Haomo; nine of the units, from the tired soil; a thousand miles begins with one
step. "I"
Today in the community to see
a friend asked, "how to become a top architect within six months,"
the netizen Road "turn off the lights to sleep, not six months ...",
indeed, a dream has come faster. As a programmer, set ambitious goals are
admirable, but not just to dream, to practice step by step down. Into a major
event, we must start from the little things; everything suddenly starting to
micro, quantitative cause a qualitative change.
We talk about today is four
launchMode Activity of.
Activity launchMode play in
multiple jumps during an important role, it can decide whether to create a new
Activity instance, whether to reuse existing Activity instance, whether other
public Activity instances where a task. Here's a brief introduction to the
concept of task, task stack structure of an object having a task can manage
multiple Activity, launch an application, it creates a corresponding task.
Activity There are four launchMode:
1.standard
2.singleTop
3.singleTask
4.singleInstance
We can configure
AndroidManifest.xml <activity> the android: launchMode property to more
than one of the four can be.
Here we introduced one by one
with an example of these four lanchMode:
1.standard
standard mode is the default
start-up mode, not the <activity> Configuration android: launchMode
property can, of course, you can also specify a value standard.
We will be an Activity, named
FirstActivity, to demonstrate what the standard startup mode. FirstActivity
code is as follows:
We FirstActivity TextView
interface for displaying the current sequence number Activity instance, Button
used to jump to the next FirstActivity interface.
Then we click on the button
several times in a row, there will be the following phenomenon:
We noted instances of
FirstActivity, but the serial numbers are different, and we need to
continuously press the Back button twice to return to the first FristActivity.
Principle standard mode as shown below:
As shown, the system will
generate a jump every time a new instance of the task FirstActivity, and put on
top of the stack structure, and when we press the Back button to see the
original FirstActivity instance.
This is the standard start-up
mode, with or without pre-existing instance, generates a new instance.
2.singleTop
We android specified property
on the basis of the above for the <activity>: launchMode =
"singleTop", the system will start mode processing in accordance with
singleTop jump behavior. We repeat the above several actions, there will be the
following phenomenon:
We see the results vary with
the standard three sequence numbers are the same, are used to say the same
FirstActivity instance; if you press the back button, the program quits
immediately, indicating the current stack structure is only one instance of
Activity ʱ?? Principle singleTop mode as shown below:
As shown above, when the
system will first look for a jump in the stack structure to see if there is an
instance of positive FirstActivity top of the stack, if there is no longer
generate new, but directly. Maybe my friends have doubts, I only see the stack
is only one Activity, if it is more Activity how to do, how, if not in the
stack? We followed through an example to confirm what everyone's questions.
We then create a new Activity
named SecondActivity, as follows:
Then the code before
FirstActivity Jump to:
Yes, FirstActivity will jump
to SecondActivity, SecondActivity will jump to FirstActivity. Demo results are
as follows:
We see two FirstActivity
serial number is different, proved to jump from SecondActivity FirstActivity
generate a new FirstActivity instance. Schematic is as follows:
We see that when the jump
from SecondActivity to FirstActivity, the system finds there FirstActivity
instance, but not at the top of the stack, then re-generate an instance.
This is singleTop start mode,
if there Activity instances corresponding right in the top of the stack, then
re-use, no longer generate new instance.
3.singleTask
In the above basis, we modify
FirstActivity attributes android: launchMode = "singleTask". The
results demonstrate the following:
We note that in the above
process, FirstActivity serial number is unchanged, SecondActivity serial number
is not the only, explanation jump from SecondActivity to FirstActivity, does
not generate a new instance, but to jump from FirstActivity SecondActivity When
generating a new instance. Schematic singleTask mode as shown below:
In the bottom half of the
figure is to jump to the results of the stack structure SecondActivity
FirstActivity after changes, we note that, SecondActivity disappeared, yes, the
jump in this process, the system found FirstActivity instance exists, so longer
generates new examples, but the Activity instance FirstActivity all over the
stack, the stack becomes FirstActivity object to display to the foreground.
Maybe my friends have questions, if you will SecondActivity also set to
singleTask mode, SecondActivity instance is not uniquely it? In our example
this is not possible, because every time you jump from SecondActivity to
FirstActivity, SecondActivity instance when forced out of the stack, jump to
the next and so on FirstActivity SecondActivity, can not find the presence of
SecondActivity instance, then you must generate the new instance. But if we
have ThirdActivity, let SecondActivity and ThirdActivity another jump, then
SecondActivity instance can guarantee unique.
This is singleTask mode, if
there are corresponding Activity instance, makes this Activity Other Activity
Examples Example above all out of the stack, so this Activity instance a stack
object display to the foreground.
4.singleInstance
This startup mode is rather
special, because it would enable a new stack structure, Acitvity placed in the
new stack structure, and ensure no other instance enters Activity.
We modified FirstActivity of
launchMode = "standard", SecondActivity of launchMode =
"singleInstance", as it relates to multiple stack structure, we need
to show id current stack structure in each Activity, so we add the following
code for each Activity:
Then we'll show you the
process:
Activity instances we found
that the two were placed in a different stack structure, on singleInstance
principle is as follows:
We see the jump from
FirstActivity to SecondActivity, re-enable a new stack frame, to place
SecondActivity instance, and then press the Back key, to return to the original
stack structure; the figure jumped again in the bottom half of SecondActivity
Go FirstActivity, this time the system will generate an original stack
structure FirstActivity instance, and then rolled back twice, attention, and
did not quit, but returned to the SecondActivity, why? Because the jump from
SecondActivity to FirstActivity time, our starting point into a stack structure
where SecondActivity instance, this way, we need to "return" to the
stack structure.
If we modify the launchMode
FirstActivity value singleTop, singleTask, singleInstance any one, the process
will be as shown:
singleInstance startup mode
is probably the most complex a model, in order to help you understand, I give
an example, if we have a share applications, which is the entrance
ShareActivity Activity, Activity also calls for other applications, we put the
Activity of Start mode is set to singleInstance, then call in other
applications. We edit ShareActivity configurations:
So then we start the Activity
in other applications:
When we open ShareActivity
then press the Back button to return to the original interface when,
ShareActivity exist as a separate entity, if the time we open share
applications, without having to create a new instance of ShareActivity to see
the results, because the system will automatically find, It exists directly.
You can print in ShareActivity about it taskId, to see the effect. On this
process, the principle is as follows: