Reprint please indicate the source :
http://blog.csdn.net/lxk_1993/article/details/50749728
This article is from: "lxk_1993 's blog";
First of all, we will introduce the launchmode of the activity. A common possession of four kinds.
1. Standard.
2.singletop.
3.singletask.
4.singleinstance.
The first standard. Either way , a new instance is created each time you start. that is, the system default startup mode.
we set the Activitya start mode to standard. Set Click to run startactivity (newIntent (Activitya. This , Activitya. class ));
Viewed two times. We see printed logcat information such as the following:
02-26 07:34:26.186 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:34:26.190 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:34:26.190 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:34:29.594 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 07:34:29.598 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:34:29.598 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:34:29.598 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:34:29.974 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onStop ()
02-26 07:34:31.030 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 07:34:31.030 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:34:31.034 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:34:31.034 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:34:31.406 16672-16672/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onStop ()
Here Activitya calls three times the OnCreate () method. So one has 3 different Activitya instances in common.
Another is the singletop, stack top multiplexing mode. It is assumed that this activitya is already at the top of the stack, and then starting Activitya from Activitya, you will not create Activitya again. Change the startup mode of Activitya to Singtop, as shown above, and print logcat information such as the following.
02-26 07:53:19.802 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:53:19.830 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:53:19.830 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:53:23.750 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 07:53:23.750 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:53:25.046 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 07:53:25.046 1953-1953/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
So it is possible to see that only one activity is created.
But assuming Activitya is not at the top of the stack, we add a activityb with a boot mode of standard. Test A start B, b at startup A.logcat information such as the following.
02-26 07:57:13.466 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:57:13.470 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:57:13.470 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:57:15.462 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 07:57:15.470 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----B----: onCreate ()
02-26 07:57:15.506 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----B----: OnStart ()
02-26 07:57:15.506 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----B----: Onresume ()
02-26 07:57:15.942 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onStop ()
02-26 07:57:16.830 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----B----: OnPause ()
02-26 07:57:16.834 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 07:57:16.834 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 07:57:16.834 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 07:57:17.226 5559-5559/com.hnpolice.xiaoke.activitylaunchmode e/----B----: onStop ()
Apparently, a Activitya was created from B to a. So with Singletop This startup mode of activity. Only this activity is at the top of the stack. Starting this activity does not create a new activity again. Otherwise there is no difference between standard and standard. And in daily development we rarely have the activity to initiate ourselves in this situation.
The Third Kind: singletask. Stack in the reuse mode. If the startup mode of a is singletask. Then there is only one instance of a in a stack .
And when a is not at the top of the stack, the start of a will directly destroy all the activity instances on the stack. We have added launchmode to standard B and C.
Then start a, start B from a, start C from B, start with C in Logcat information such as the following.
02-26 08:17:27.358 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
02-26 08:17:27.366 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
02-26 08:17:27.366 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
02-26 08:17:29.042 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnPause ()
02-26 08:17:29.046 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----B----: onCreate ()
02-26 08:17:29.074 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----B----: OnStart ()
02-26 08:17:29.074 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----B----: Onresume ()
02-26 08:17:29.474 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onStop ()
02-26 08:17:30.498 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----B----: OnPause ()
02-26 08:17:30.502 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----C----: onCreate ()
02-26 08:17:30.506 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----C----: OnStart ()
02-26 08:17:30.506 24079-24079/com.hnpolice.xiaoke.activitylaunchmode e/----C----: Onresume ()
02-26 08:17:30.910 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----B----: onStop ()
02-26 08:17:32.414 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----B----: OnDestroy ()
02-26 08:17:32.422 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----C----: onPause ()
02-26 08:17:32.422 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----A----: Onrestart ()
02-26 08:17:32.422 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----A----: OnStart ()
02-26 08:17:32.422 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----A----: onresume ()
02-26 08:17:32.790 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----C----: onStop ()
02-26 08:17:32.790 24079-24079/com.hnpolice.xiaoke.activitylaunchmodee/----C----: OnDestroy ()
From Logcat information we can see that when starting a from C, the activity instance between a and C in the stack is destroyed before another start of a, that is, C's OnPause (). Then destroy C when you finish starting a. If A is a singtask mode, BCDE are standard mode. Then start ABCDE one at a time, and then start a. Before the OnPause method of E, the OnStop and OnDestroy methods of B, C, d are followed. Then when you finish starting a, call E's OnStop and OnDestroy to destroy E.
05-11 06:11:39.031 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: onCreate ()
05-11 06:11:39.099 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
05-11 06:11:39.103 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
05-11 06:11:44.391 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----A----: onPause ()
05-11 06:11:44.427 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----B----: onCreate ()
05-11 06:11:44.475 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----B----: OnStart ()
05-11 06:11:44.475 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----B----: Onresume ()
05-11 06:11:44.919 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----A----: onStop ()
05-11 06:11:46.431 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----B----: onPause ()
05-11 06:11:46.447 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----C----: onCreate ()
05-11 06:11:46.459 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----C----: OnStart ()
05-11 06:11:46.459 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----C----: Onresume ()
05-11 06:11:46.851 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----B----: onStop ()
05-11 06:11:47.303 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----C----: onPause ()
05-11 06:11:47.327 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----D----: onCreate ()
05-11 06:11:47.339 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----D----: OnStart ()
05-11 06:11:47.339 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----D----: Onresume ()
05-11 06:11:47.735 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----C----: onStop ()
05-11 06:11:51.235 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----B----: OnDestroy ()
05-11 06:11:51.243 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----C----: OnDestroy ()
05-11 06:11:51.243 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----D----: OnPause ()
05-11 06:11:51.247 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onrestart ()
05-11 06:11:51.247 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: OnStart ()
05-11 06:11:51.247 1060-1060/com.hnpolice.xiaoke.activitylaunchmode e/----A----: Onresume ()
05-11 06:11:51.619 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----D----: onStop ()
05-11 06:11:51.619 1060-1060/com.hnpolice.xiaoke.activitylaunchmodee/----D----: OnDestroy ()
Such a pattern will be used when the program exits in development.
A program exit method commonly known as "lazy-man".
is to set the start mode of the first interface to Singletask. Then listen to the back key.
First Prompt "Press again to quit the program", the second time directly finish off the first interface. The program exits and is finished.
The fourth mode of startup: SingleInstance. Such a pattern is the singletask mode of reinforcement.
In addition to all the features of Singletask. It also stipulates that the activity of such a pattern can only be located in a single task stack alone.
If you're not sure, you can look at this. Here is an article http://blog.csdn.net/liuhe688/article/details/6754323. The specific description of the activity of the Launchmode.
Welcome to comment, Spit groove, shoot bricks. If you think the writing is good, help top up, thank you.
Friendship Link:
Let's say you don't know the life cycle of acitivity very well, you can point here:
the life cycle of the activity is explained in detail: http://blog.csdn.net/lxk_1993/article/details/50731594.
The previously mentioned program exits the method link,
Android Exit app The most elegant way: http://mp.weixin.qq.com/s?__biz=MzA3MDMyMjkzNg==&mid=402499969&idx=2&sn= C74c57d582fee85cc990ee086e7a1c40&scene=23&srcid=0111jjrfatywz1wtod0n1l1r#rd
Android Development Art Explore learning combine activity life cycle understanding activity's Launchmode