1.简介
clientlifecyclemanager是管理activity lifecycle的,在activitytaskmanagerservice里面提供getlifecyclemanager来获取此对象,其中mlifecyclemanager是在activitytaskmanagerservice的构造方法里面初始化的
本文基于android 10的源代码来分析说明
clientlifecyclemanager getlifecyclemanager() {
return mlifecyclemanager;
}
整体类图如下:
相关类功能说明:
- clienttransctionitem对象,一个回调消息,client端可以调用执行,实现了baseclientrequest接口,在接口里面定义了3个方法,preexecute/execute/poseexecute方法
- activitylifecycleitem继承自clienttransctionitem,主要的子类有destoryactivityitem、pauseactivityitem、stopactivityitem、resumeactivityitem
- clienttransaction
是一个容器,持有一系列可以发送给client的消息,包括有callback列表和一个目标状态lifecycle
/** a list of individual callbacks to a client. */
@unsupportedappusage
private list<clienttransactionitem> mactivitycallbacks;
/**
* final lifecycle state in which the client activity should be after the transaction is
* executed.
*/
private activitylifecycleitem mlifecyclestaterequest;
/** target client. */
private iapplicationthread mclient;
/** target client activity. might be null if the entire transaction is targeting an app. */
private ibinder mactivitytoken;
- transactionexecutor 管理变换的执行,
1.clienttransactionhandler用来调用activitythread的实现方法
2.execute 方法里面会executecallbacks和executelifecyclestate,
3.cycletopath和performlifecyclesequence这2个方法用来辅助完成executelifecyclestate,其中cycletopath把要转换的状态都加到一个intarray数组里面,performlifecyclesequence依次来执行intarray里面的方法,调用activitythread的实现方法
2.clientlifecyclemanager管理状态变化流程
从clienttransaction的定义来看,变换主要有2种:mactivitycallbacks和mlifecyclestaterequest。
- startpausinglocked方法里面,只涉及到addcallback
- realstartactivitylocked里面涉及到mlifecyclestaterequest
以下以startpausinglocked和realstartactivitylocked的调用流程来说明
2.1 startpausinglocked
在startpausinglocked方法里面,只执行了callback方法
mservice.getlifecyclemanager().scheduletransaction(prev.app.getthread(),
prev.apptoken, pauseactivityitem.obtain(prev.finishing, userleaving,
prev.configchangeflags, pauseimmediately));
scheduletransaction 传入的callcack对象是pauseactivityitem,通过obtain方法来获取,传入了activitythread和activityrecord.token对象。
在clientlifecyclemanager类里面,定义了4个scheduletransaction方法重载方法,最终都调用到如下方法,
void scheduletransaction(clienttransaction transaction) throws remoteexception {
final iapplicationthread client = transaction.getclient();
transaction.schedule();
}
继续调用clienttransaction 的schedule方法,mclient是activitythread对象,activitythread未定义此方法,在其父类clienttransactionhandler定义了此方法,通过handler机制,最终调用到了
//clienttransaction.java
public void schedule() throws remoteexception {
mclient.scheduletransaction(this);
}
//clienttransactionhandler.java
void scheduletransaction(clienttransaction transaction) {
transaction.preexecute(this); // 调用callback的preexecute方法
sendmessage(activitythread.h.execute_transaction, transaction);
}
//activitythread.java
case execute_transaction:
final clienttransaction transaction = (clienttransaction) msg.obj;
mtransactionexecutor.execute(transaction);
在前面有过介绍,transactionexecutor 负责管理变换的执行,核心实现在这个里面
public void execute(clienttransaction transaction) {
...
// 执行mactivitycallbacks里面的callcack
executecallbacks(transaction);
// 执行mlifecyclestaterequest的变换,由于没有设置,此方法直接返回
executelifecyclestate(transaction);
mpendingactions.clear();
if (debug_resolver) slog.d(tag, tid(transaction) "end resolving transaction");
}
public void executecallbacks(clienttransaction transaction) {
final list<clienttransactionitem> callbacks = transaction.getcallbacks();
for (int i = 0; i < size; i) {
final clienttransactionitem item = callbacks.get(i);
// 执行callback的execute和postexecute方法
item.execute(mtransactionhandler, token, mpendingactions);
item.postexecute(mtransactionhandler, token, mpendingactions);
}
}
2.1 startpausinglocked
在realstartactivitylocked方法里面,同时添加了callback和目标lifecycle
clienttransaction.addcallback(launchactivityitem.obtain(new intent(r.intent),
system.identityhashcode(r), r.info,
// todo: have this take the merged configuration instead of separate global
// and override configs.
mergedconfiguration.getglobalconfiguration(),
mergedconfiguration.getoverrideconfiguration(), r.compat,
r.launchedfrompackage, task.voiceinteractor, proc.getreportedprocstate(),
r.icicle, r.persistentstate, results, newintents,
dc.isnexttransitionforward(), proc.createprofilerinfoifneeded(),
r.assisttoken));
// set desired final state.
final activitylifecycleitem lifecycleitem;
if (andresume) { // 走的这个分支
lifecycleitem = resumeactivityitem.obtain(dc.isnexttransitionforward());
} else {
lifecycleitem = pauseactivityitem.obtain();
}
clienttransaction.setlifecyclestaterequest(lifecycleitem);
// schedule transaction.
mservice.getlifecyclemanager().scheduletransaction(clienttransaction);
前面的调用流程是一样的,lifecyclemanager.scheduletransaction ==> clienttransaction.schedue ==> clienttransactionhandler.scheduletransaction ==> transactionexecutor.execute
public void execute(clienttransaction transaction) {
...
// 执行mactivitycallbacks里面的callcack
executecallbacks(transaction);
// 执行mlifecyclestaterequest的变换
executelifecyclestate(transaction);
mpendingactions.clear();
if (debug_resolver) slog.d(tag, tid(transaction) "end resolving transaction");
}
public void executecallbacks(clienttransaction transaction) {
final list<clienttransactionitem> callbacks = transaction.getcallbacks();
for (int i = 0; i < size; i) {
final clienttransactionitem item = callbacks.get(i);
// 执行callback的execute和postexecute方法
item.execute(mtransactionhandler, token, mpendingactions);
item.postexecute(mtransactionhandler, token, mpendingactions);
}
}
private void executelifecyclestate(clienttransaction transaction) {
// 获取 mlifecyclestaterequest
final activitylifecycleitem lifecycleitem = transaction.getlifecyclestaterequest();
//如果没有通过setlifecyclestaterequest设置,由直接返回
if (lifecycleitem == null) {
// no lifecycle request, return early.
return;
}
// cycle to the state right before the final requested state.
cycletopath(r, lifecycleitem.gettargetstate(), true /* excludelaststate */, transaction);
// execute the final transition with proper parameters.
lifecycleitem.execute(mtransactionhandler, token, mpendingactions);
lifecycleitem.postexecute(mtransactionhandler, token, mpendingactions);
}
private void cycletopath(activityclientrecord r, int finish, boolean excludelaststate,
clienttransaction transaction) {
final int start = r.getlifecyclestate();
// 通过mhelper来获取lifecyclepath,此方法会根据start和finish状态来构造一个状态数组
// 1. start状态是on_create:由于launchactivityitem会调用handlelaunchactivity方法将状态设置为on_create
// r.setstate(on_create);
// 2. finish状态是on_resume:在resumeactivityitem里面,gettargetstate直接返回的on_resume
final intarray path = mhelper.getlifecyclepath(start, finish, excludelaststate);
performlifecyclesequence(r, path, transaction);
}
// 根据不同的状态,调用activitythread的对应方法
private void performlifecyclesequence(activityclientrecord r, intarray path,
clienttransaction transaction) {
final int size = path.size();
for (int i = 0, state; i < size; i) {
state = path.get(i);
switch (state) {
case on_create:
mtransactionhandler.handlelaunchactivity(r, mpendingactions,
null /* customintent */);
break;
case on_start:
mtransactionhandler.handlestartactivity(r, mpendingactions);
break;
case on_resume:
mtransactionhandler.handleresumeactivity(r.token, false /* finalstaterequest */,
r.isforward, "lifecycler_resume_activity");
break;
case on_pause:
mtransactionhandler.handlepauseactivity(r.token, false /* finished */,
false /* userleaving */, 0 /* configchanges */, mpendingactions,
"lifecycler_pause_activity");
break;
case on_stop:
mtransactionhandler.handlestopactivity(r.token, false /* show */,
0 /* configchanges */, mpendingactions, false /* finalstaterequest */,
"lifecycler_stop_activity");
break;
case on_destroy:
mtransactionhandler.handledestroyactivity(r.token, false /* finishing */,
0 /* configchanges */, false /* getnonconfiginstance */,
"performlifecyclesequence. cycling to:" path.get(size - 1));
break;
case on_restart:
mtransactionhandler.performrestartactivity(r.token, false /* start */);
break;
default:
throw new illegalargumentexception("unexpected lifecycle state: " state);
}
}
}
重点说明一下 cycletopath方法
- start状态是on_create:由于launchactivityitem会调用handlelaunchactivity方法将状态设置为on_create, r.setstate(on_create);
- finish状态是on_resume:在resumeactivityitem里面,gettargetstate直接返回的on_resume
- 在transactionexecutorhelper.getlifecyclepath方法里面,会依次加入on_start和on_resume
在execute方法里面
- executecallbacks 方法
会调用launchactivityitem的execute方法来handlelaunchactivity,最终会调用activity.oncreate - executelifecyclestate方法
会依次调用handlestartactivity和handleresumeactivity,最终调用activity.onstop和acitivty.onresume