对 launchMode的简单测试:
三个Activity, A, B, C, 分别属于不同的Application
B 和 C 都通过Intent setAction来startActivity A,
在launchMode是缺省<standard>的每次都实例化的case下:
B先启动A<进程: 4082>, A所在的Application被构造<进程: 4120>, A 构造函数被调用<进程: 4120>。
然后菜单键回到launcher, 启动C,C启动A<进程: 4193>, A的构造函数被调用<进程: 4120>,
再且切回到B, B所在的task stack 依然停留在A上。 stakc结构 BA
如果长按菜单键切到任务列表,发现只有2个任务,2个任务的栈顶Activity都是A.
结论:
可见,从一个App的Activity A 调用 另一个App的Activity B,
首先,如果B所在的App还没有组件(Activity, Service, ContentProvider, Receiver)被构造过, 那么B所在的Application会在
一个单独自己的进程<V>进行构造,然后也在该进程构造出B,
而对于缺省的launchMode<standard>, 如果这时候Activity C也调用了B, 那么会在进程V中重新构造实例一个B.
Android的Task其实一个忽略了进程界限的概念,位于不同进程的Activity组成一个Task,是跨进程的。在Task层面,进程没有意义。
一般来说,每个Application有自己的进程.
然后试一下 launchMode = “singleTask”
同样上面步骤
和上面的区别在于:
C启动A时,A没有再被构造<因为single了>,
而切回到B,发现B所在的task stack 回退到了B。stack结构 B.
而且到C,发现C的也回退到了C,stack里只有C了.
如果长按菜单键切到任务列表,发现只有3个任务,3个任务的栈顶分别是A, B, C.
上面这两个都可以理解,因为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.
因为会创建一个新的Task, 而这个Task里面, A是这个Task的root activity,
因此,在 B/C所在的Task其实没有增加新的Activity A, 所以在切到B/C时,才没有A在上面.
不过有个重要的Note:
Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
虽然不同的Task了,但是BackButton保证了Activity切换的连贯性. 突破了之前只能在一个Task内back的限制.
singleInstance和singleTask的主要区别就在于这个新创建的Task,如果 A又调用了别的Activity D,那么
singleInstance会为D再创建一个Task保证A所在的Task只有A一个,而singleTask则会在当前A所在的Task加入D.
From: http://developer.android.com/guide/components/tasks-and-back-stack.html
Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Back button always takes the user to the previous activity. However, if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.
Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.