新说android launch mode

可能大家都知道android有4种launch mode分别是:

  1. standard

  2. singleTop

  3. singleTask

  4. singleInstance

很多中文博客中解释的不是那么清楚而且不是那么准确,今天抽空总结一下android的原文说法

  • standard

Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

默认的模式。每次访问此类标识的activity都会在task中创建一个实例。可以在不同的多个task中被多次创建,一个task中可以有多个实例。

总结:没有任何限制的new

  • singleTop

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 activity at the top of the back stack is not an existing instance of the activity).

顶部单例模式。如果task的顶部是此类标识的activity,那么再次访问该activity时不会创建实例,只会请求一次 onNewIntent方法。除此条件之外,和standard没有区别。

  • singleTask

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

底部单例模式。如果是此类标识的activity,那么该activity一定是在一个task底部的,没有其他情况。当task中没有此类activity时,系统会创建一个新的task以供该activity实例化,如果已经创建了新的task并且该activity是栈底位置,那么会调用该activity的onNewIntent方法而不会创建实例,这种activity在同一时间只能有一个实例。

  • singleInstance

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

单例模式。如果是此类标识的activity,创建时系统会新建一个task,并且不会往这个task中加入其他的activity,供该activity独享。和singeTask的区别是singeTask的task中还能加入其他activity,而它,只能是独孤求败。。。


顺便说一句:back stack跟task是不同的概念,别搞混了


你可能感兴趣的:(android)