Android Common Activity start mode (LaunchMode) and usage scenarios

First, why the need to start mode

In Android development, we all know that in case of default, is the same if we start an Activity, the system will create multiple instances and put them one by one into the task stack. When we click on the return (back) key, these examples Activity turn to remove a task from the stack, following the principle of "last in, first out" (last out).

Here we consider a problem when we start with a multiple Activity, the system will create multiple instances into the task stack, so is not it consumes memory resources? To solve this problem, Android provides startup mode Actiivty.

Activity startup mode, there are four: standard, singleTop, singleTask and singleInstance.

Second, start the pattern classification

1. standard: Standard Model

This startup mode to the standard mode is the default mode. Whenever we start an Activity, the system will create a corresponding instance, regardless of whether this instance already exists. This model, a stack can have multiple instances, each also has its own task stack. And who started this Activity, then the Activity will run in the stack starts its Activity is located.

5777390-a8b256a1bcfadde2.png


5777390-43ce4ca031748a56.png

First Jump


5777390-b7da94fcc4fa8183.png

The second jump


5777390-eb632e19cabe624f.png

As can be seen from the results, each will re-create a new instance, when you select the return key, will step by step in, last-out order activity will launch stack

2, singleTop: multiplexing mode stack

In this startup mode, if you want to start Activity has been at the top of the stack, so this time the system will not create a new instance, but directly open the page, while its onNewIntent () method will be executed, we can Intent carried out by value, and its onCreate (), onStart () method is never called, because it has not changed any. But if there is no top of the stack, it will still create a new instance again, so there will be problems of duplicate pages.


5777390-51b1c2d7c195bc70.png
5777390-3b84cff5c608f2a8.png
5777390-027cc23a1a0e516a.png


5777390-cffe6c4938a00feb.png


5777390-78cdba65ee1bf262.png


The following is a presentation if not the top of the stack, namely middle to jump to other pages, and then jump all its own, will find the system to re-initialize an instance, rather than simply reading shows

5777390-0de7eaf1f98d0464.png


5777390-64274e0693bda78c.png


5777390-48c0be172de527c2.png


5777390-4ee92d10543164e1.png

scenes to be used:

这种模式应用场景的话,假如一个新闻客户端,在通知栏收到了3条推送,点击每一条推送会打开新闻的详情页,如果为默认的启动模式的话,点击一次打开一个页面,会打开三个详情页,这肯定是不合理的。如果启动模式设置为singleTop,当点击第一条推送后,新闻详情页已经处于栈顶,当我们第二条和第三条推送的时候,只需要通过Intent传入相应的内容即可,并不会重新打开新的页面,这样就可以避免重复打开页面了

3、singleTask:站内复用模式

在这个模式下,如果栈中存在这个Activity的实例就会复用这个Activity,不管它是否位于栈顶,复用时,会将它上面的Activity全部出栈,因为singleTask本身自带clearTop这种功能。并且会回调该实例的onNewIntent()方法。其实这个过程还存在一个任务栈的匹配,因为这个模式启动时,会在自己需要的任务栈中寻找实例,这个任务栈就是通过taskAffinity属性指定。如果这个任务栈不存在,则会创建这个任务栈。不设置taskAffinity属性的话,默认为应用的包名。

在复用的时候,首先会根据taskAffinity去找对应的任务栈:

1、如果不存在指定的任务栈,系统会新建对应的任务栈,并新建Activity实例压入栈中。

2、如果存在指定的任务栈,则会查找该任务栈中是否存在该Activity实例

      a、如果不存在该实例,则会在该任务栈中新建Activity实例。

      b、如果存在该实例,则会直接引用,并且回调该实例的onNewIntent()方法。并且任务栈中该实例之上的Activity会被全部销毁。

使用场景:

SingleTask这种启动模式最常使用的就是一个APP的首页,因为一般为一个APP的第一个页面,且长时间保留在栈中,所以最适合设置singleTask启动模式来复用。


5777390-ccf7824ed9894ac8.png

4、singleInstance:单实例模式

Examples of single-mode, by definition, only one instance. The model includes all of the features singleTask mode, and its difference is, Activity in this mode will occupy a separate Task stack is globally unique, so that the whole system in one example, since the characteristics of the stack multiplexed , subsequent requests will not create a new Activity instance, unless this particular task stack is destroyed. Activity singleInstance mode started in the entire system is in a single embodiment, at startup if such Activiyt, an instance already exists, then it will schedule the tasks to the foreground, to reuse this example. That is, create a separate task stack, when a number of different tasks stack exists, select the return key, you will get back on a task stack and display on a task stack top of the page, but other pages before the top of the page again select the return key is not showing.

Activity start the pattern when the system looks for the existence of:

1, does not exist, the first task will be to create a new stack, followed by the creation of the Activity instance.

2, is present, it will direct reference to this example, and the callback onNewIntent () method.

Special case: the task or the stack is destroyed, then the system will be re-created.

scenes to be used:

Very often, the telephone dial page, through their own applications or other applications open call page, as long as the system stack instance exists, it will be called directly.

Reproduced in: https: //www.jianshu.com/p/72a0900cb8e1

Guess you like

Origin blog.csdn.net/weixin_33836874/article/details/91244155