在Android P中,ActivityThread extends ClientTransactionHandler,而ClientTransactionHandler封装了handlexxxActivity的方法。
而AMS控制Activity的生命周期,通过ApplicationThread#scheduleTransaction(ClientTransaction),而去到应用进程后,具体执行哪个生命周期,看ClientTransaction#addCallback(ClientTransactionItem)中的ClientTransactionItem是什么子类型,还有ClientTransaction.setLifecycleStateRequest(ActivityLifecycleItem);其中可以有多个callback。LaunchActivityItem就是要执行LaunchActivity任务,而PauseActivity则是PauseActivityItem,StopActivity则是StopActivityItem,Destroy Activity则是DestoryActivityItem。
ClientTransactionItem implements BaseClientRequest, Parcelable
ActivityLifecycleItem extends ClientTransactionItem
LaunchActivityItem extends ClientTransactionItem
ResumeActivityItem exttends ActivityLifecycleItem
PauseActivityItem extends ActivityLifecycleItem
StopActivityItem extends ActivityLifecycleItem
DestroyActivityItem extends ActivityLifecycleItem
可以看到AMS能把ClientTransactionItem 传到应用进程是因为它实现了Parcelable。
ApplicationThread#scheduleTransaction(ClientTransaction):
public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
ActivityThread.this.scheduleTransaction(transaction);
}
ClientTransactionHandler#scheduleTransaction()
void scheduleTransaction(ClientTransaction transaction) {
transaction.preExecute(this);
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}
ActivityThread.H.EXECUTE_TRANSACTION的处理:
case EXECUTE_TRANSACTION:
final ClientT