The author recently made a project to use the activity of the starting mode, but also for the first time profoundly understand its strong and convenient. Here is also to share their own income with you, wrote a relatively simple demo, easy for everyone to understand.
This blog is intended to let the startup mode does not understand the developers have a more image of the understanding, as for the deep inquiry, I still recommend to see Ningyugang predecessors wrote "Android Development art exploration."
on-line The activity's startup mode explains a lot of blogs, but most of them need to master the "stack" of knowledge, and many are not so easy to understand. The author intends to make a difference, on the one hand through the Baidu map to speak its role , on the other hand, through their own demo demo to explain the 4 mode of startup .
Role:
Everyone on the Android Baidu map must be very familiar with, let us look at the following image: (Baidu map)
assuming that the above 3 pictures are in turn ABC, the author obtains the order of the three graphs are: (Curious readers can try their own)
Open a, open B, open C, open B, close B, close C, go back to a, exit the program. (The return key is used for closing)
You can carefully observe a and C two map, it is not difficult to find that this is actually the same activity, just by dynamically changing the layout so that people are not aware of.
So the question is, how do we reuse the same activity?
is undoubtedly using the activity's startup mode skillfully.
According to the normal startup mode, a open b,b and then open C, there should be 3 activity, but obviously, we have only 2 in this test activity, the evidence is in the C picture when the return button did not jump to another activity, but changed the layout, Then press the return key to eject the program.
What a practical skill it is! If this is the case, we will never have to do all kinds of brain-free finish ()!
Yes, that's a function of the activation mode of activity.
(as to whether there is any other role, the author has not yet grasped, welcome guidance)
Demo Tutorial:
First of all we need to know that there are four startup modes: Standard (Normal mode), Singletop (stack top multiplexing mode), Singletask (on-stack multiplexing mode), SingleInstance (singleton mode).
The novice may ask where the startup mode is set, the author answers here:
In Androidmanifest, the activity's Launchmode property can be set directly.
Standard : Standard mode, which is also the default mode for the system.
Each time an activity is started, a new instance is recreated, regardless of whether the instance exists.
, the activity that I opened in turn is, Red,blue,green,green. Then press the return key to close the order, respectively, green,green,blue,red.
Singletop: Stack top multiplexing mode.
In fact, the explanation is very simple, like the above standard , we can find that Green's activity can restart a green activity, he can actually jump to himself, is simply ridiculous, wasting memory. Once you've set up singletop, you won't be able to create a new activity if you have the "Jump yourself" action.
, the author of Red's activity set the singletop startup mode.
The activity from blue jumps to Red's activity, but when red itself jumps to itself, it does not create a new instance again.
Singletask: In-stack multiplexing mode.
This startup mode is more convenient, more like the author of the above-described Baidu map of the map interface of the start mode. In this mode, if the activity has already been created, it will not be created again, but will be used to directly use the previously created instance.
, the author Blur's activity set the the singletask startup mode.
The author, in turn, played the activity for, Red,blue,green,red,blue. The attentive classmate has found that when the return button is finally pressed back to red, it exits the activity, stating that only the red one activity is left.
Because singletask default has cleartop effect, such as ADBC 4 activity, if D is singletask, then when the D is started again, will only leave the ad two Activity, the middle of The activity has been clear. (This point if you do not know you can dig into it yourself OH)
SingleInstance: Singleton mode.
This pattern is really more complicated without the idea of "stack". But if even the "stack" do not understand, then generally do not use the starting mode here.
A little explanation: in the default startup mode, when we start the activity, the system creates multiple instances and puts them all into the people station, and when we press the back key, the activity is returned. In singleinstance, however, theactivity of SingleInstance is to create a new task stack directly and run it on its own, and because of the nature of the reusability within the stack, No new activity is created after the next post.
The author of this blog is intended to allow beginners to better understand the activation mode of activity, there may be many places are not rigorous enough, such as the predecessors of the generous advice, not very grateful.
Demonstration demo:http://download.csdn.net/detail/double2hao/9466241
A brief analysis of the function of Android activity's starting mode +demo