1. 程式人生 > >探索Activity啟動模式

探索Activity啟動模式

LifecycleActivity記為A,LifecycleActivity2記為B

一, Standard

略.

二, SingleTop

開啟App,啟動第一個Activity(A1),看到taskId == 2215

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2215
D/PRETTY_LOGGER: │ numActivities == 1
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

啟動A2 

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2215
D/PRETTY_LOGGER: │ numActivities == 2
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

啟動B(singleTop) 

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2215
D/PRETTY_LOGGER: │ numActivities == 3
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity2
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

再次啟動B,B沒有創新新例項,而是回調了onNewIntent方法

D/PRETTY_LOGGER: │ Instrumentation.callActivityOnNewIntent  (Instrumentation.java:1215)
D/PRETTY_LOGGER: │    LifecycleActivity2.onNewIntent  (LifecycleActivity2.kt:48)

在B中,啟動A,記為A3

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2215
D/PRETTY_LOGGER: │ numActivities == 4
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A3中,啟動B,例項數增加1,說明不再頂部的B沒有得到複用.

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2215
D/PRETTY_LOGGER: │ numActivities == 5
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity2
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

 

總結:singleTop,是棧內複用. 

如果某activity為singleTop,

如果它位於棧頂,則回撥onNewIntent(),而不新建例項;

如果不位於棧頂,則新建Activity.

三, SingleTask

開啟App,啟動第一個Activity(A1),看到taskId == 2212

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2212
D/PRETTY_LOGGER: │ numActivities == 1
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A1中,啟動第二個Activity(A2) ,看到taskId不變,activity例項數為2

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2212
D/PRETTY_LOGGER: │ numActivities == 2
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A2中,啟動第三個Activity(B),看到taskId仍不變.activity例項數為3

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2212
D/PRETTY_LOGGER: │ numActivities == 3
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity2
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

 在B中,嘗試啟動B,看到B回調了onNewIntent方法

D/PRETTY_LOGGER: │ Instrumentation.callActivityOnNewIntent  (Instrumentation.java:1215)
D/PRETTY_LOGGER: │    LifecycleActivity2.onNewIntent  (LifecycleActivity2.kt:48)

在B中,啟動A,記為A3看到taskId仍不變,activity例項數增加1

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2212
D/PRETTY_LOGGER: │ numActivities == 4
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A3中啟動B,此時A3出棧,B出現在棧頂.說明B(singleTask)在棧內複用了,棧內B之上的Activity都出棧.

然後,後續aciivity依次出棧.

總結:singleTask,棧內複用.

如果某Activity為singleTask

如果它位於棧頂,則不建立新例項,它在棧頂被複用;

如果位於棧中,則其上部所有Activity都出棧,該Activity置於棧頂,此為棧內複用.

四, SingleInstance

開啟App,啟動第一個Activity(A1),看到taskId == 2208

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2208
D/PRETTY_LOGGER: │ numActivities == 1
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A1中,啟動第二個Activity(A2) ,看到taskId不變,仍為2208

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2208
D/PRETTY_LOGGER: │ numActivities == 2
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

在A2中,啟動第三個Activity(B),看到taskId == 2209,已經發生變化

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2209
D/PRETTY_LOGGER: │ numActivities == 1
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity2
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity2

我們在B中,再次啟動B,看到B回調了onNewIntent方法,而且numActivities仍為1,說明沒有建立新例項.

D/PRETTY_LOGGER: │ Instrumentation.callActivityOnNewIntent (Instrumentation.java:1215)
D/PRETTY_LOGGER: │ LifecycleActivity2.onNewIntent  (LifecycleActivity2.kt:48)

然後,在B中,啟動A,看到回到了任務棧A中,activity的例項數增加1,此時的Activity數表示taskId為2208的activity數.

D/PRETTY_LOGGER: │ taskCount == 1
D/PRETTY_LOGGER: │ taskId == 2208
D/PRETTY_LOGGER: │ numActivities == 3
D/PRETTY_LOGGER: │ topActivity == .activity.LifecycleActivity
D/PRETTY_LOGGER: │ baseActivity == .activity.LifecycleActivity

然後,點選返回按鈕.發現,taskId(2208)的activity依次出棧,最後task(2209)的activity出棧.(圖略)

總結:singleInstance,建立新的任務棧,而且該Activity獨佔這個任務棧.