startActivity startActivity有很多重载方法,最终都会调用startActivityForResult
然后的调用过程1 2 -> Activit.startActivityForResult(...) -> Instrumentation.execStartActivity(...)
在 Instrumentation.execStartActivity
调用了 ActivityManager.getService().startActivity(...)
,后续的流程进入到了系统进程
1 2 3 4 5 6 -> ActivityManagerService.startActivity(...) -> ...startActivityAsUser(...) -> ActivityStarter.execute() -> ...startActivityMayWait() -> ...startActivity() -> ...startActivityUnchecked(...)
startActivityUnchecked
是一个很重要的方法,这个方法里会根据启动标志位和Activity启动模式来决定如何启动一个Activity以及是否要调用deliverNewIntent方法通知Activity有一个Intent试图重新启动它。 因为这里只讨论住 activity 启动的主流程,所以先不看这个。
startActivityUnchecked
之后的调用过程如下
1 2 3 4 -> ActivityStarter.startActivityUnchecked(...) -> ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(...) -> ActivityStack.resumeTopActivityUncheckedLocked(...) -> ...resumeTopActivityInnerLocked(...)
在 resumeTopActivityInnerLocked
中会先对 resume 状态的 activity 执行 pause。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private boolean resumeTopActivityInnerLocked (ActivityRecord prev, ActivityOptions options) { ... boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false ); if (mResumedActivity != null ) { pausing |= startPausingLocked(userLeaving, false , next, false ); } ... mStackSupervisor.startSpecificActivityLocked(next, true , true ); try { next.completeResumeLocked(); } catch (Exception e) { ... } ... return true ; }
activity 的 pause 过程 startPausingLocked
之后会执行 startPausingLocked
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 final boolean startPausingLocked (boolean userLeaving, boolean uiSleeping, ActivityRecord resuming, boolean pauseImmediately) { ... if (prev.app != null && prev.app.thread != null ) { try { mService.updateUsageStats(prev, false ); mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken, PauseActivityItem.obtain(prev.finishing, userLeaving, prev.configChangeFlags, pauseImmediately)); } catch (Exception e) { ... } } else { mPausingActivity = null ; mLastPausedActivity = null ; mLastNoHistoryActivity = null ; } ... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 void scheduleTransaction (@NonNull IApplicationThread client, @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest) throws RemoteException { final ClientTransaction clientTransaction = transactionWithState(client, activityToken, stateRequest); scheduleTransaction(clientTransaction); } private static ClientTransaction transactionWithState (@NonNull IApplicationThread client, @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest) { final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken); clientTransaction.setLifecycleStateRequest(stateRequest); return clientTransaction; } void scheduleTransaction (ClientTransaction transaction) throws RemoteException { final IApplicationThread client = transaction.getClient(); transaction.schedule(); if (!(client instanceof Binder)) { transaction.recycle(); } }
Android 9.0在这里引入了ClientLifecycleManager
和 ClientTransactionHandler
来辅助管理Activity生命周期,
startPausingLocked
生成了一个 PauseActivityItem
然后,ClientLifecycleManager
会将参数打包为一个 ClientTransaction
并设置一个改变生命周期的request。
1 2 3 4 public void schedule () throws RemoteException { mClient.scheduleTransaction(this ); }
schedule()
将工作转给了 mClient ,mClient 是 transactionWithState 中传入的,是一个 IApplicationThread,这是一个APP进程的binder对象。 在 pause 流程中代表要pause的activity所在的进程。 IApplicationThread 的实现是 ActivityThread 的内部类 ApplicationThread 。这里通过binder调用从 系统进程转到了应用进程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Override public void scheduleTransaction (ClientTransaction transaction) throws RemoteException { ActivityThread.this .scheduleTransaction(transaction); } void scheduleTransaction (ClientTransaction transaction) { transaction.preExecute(this ); sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction); } private void sendMessage (int what, Object obj, int arg1, int arg2, boolean async) { Message msg = Message.obtain(); ... if (async) { msg.setAsynchronous(true ); } mH.sendMessage(msg); }
H extends Handler 在上面可以看出,最终,向 mH 发送了一个 message
1 2 3 4 5 6 7 8 9 10 11 public void handleMessage (Message msg) { switch (msg.what) { ... case EXECUTE_TRANSACTION: final ClientTransaction transaction = (ClientTransaction) msg.obj; mTransactionExecutor.execute(transaction); ... break ; ... } }
msg.what 是 ActivityThread.H.EXECUTE_TRANSACTION
。将activity生命周期的变换任务交给了 TransactionExecutor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public void execute (ClientTransaction transaction) { final IBinder token = transaction.getActivityToken(); executeCallbacks(transaction); executeLifecycleState(transaction); mPendingActions.clear(); log("End resolving transaction" ); } private void executeLifecycleState (ClientTransaction transaction) { final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest(); ... cycleToPath(r, lifecycleItem.getTargetState(), true ); lifecycleItem.execute(mTransactionHandler, token, mPendingActions); lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions); }
在 TransactionExecutor 中从 ClientTransaction 获取 ActivityLifecycleItem , 并执行 execute 和 postExecute 。 由于我们传入的是 PauseActivityItem , 所以真正执行的代码还是在系统进程的 PauseActivityItem 里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Override public void execute (ClientTransactionHandler client, IBinder token,PendingTransactionActions pendingActions) { client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions, "PAUSE_ACTIVITY_ITEM" ); } @Override public void postExecute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { if (mDontReport) { return ; } try { ActivityManager.getService().activityPaused(token); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Override public void handlePauseActivity (IBinder token, boolean finished, boolean userLeaving, int configChanges, PendingTransactionActions pendingActions, String reason) { ActivityClientRecord r = mActivities.get(token); if (r != null ) { if (userLeaving) { performUserLeavingActivity(r); } r.activity.mConfigChangeFlags |= configChanges; performPauseActivity(r, finished, reason, pendingActions); if (r.isPreHoneycomb()) { QueuedWork.waitToFinish(); } mSomeActivitiesChanged = true ; } } private void performPauseActivityIfNeeded (ActivityClientRecord r, String reason) { try { r.activity.mCalled = false ; mInstrumentation.callActivityOnPause(r.activity); } ... r.setState(ON_PAUSE); }
1 2 3 4 public void callActivityOnPause (Activity activity) { activity.performPause(); }
新activity启动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void startSpecificActivityLocked (ActivityRecord r, boolean andResume, boolean checkConfig) { ProcessRecord app = mService.getProcessRecordLocked(r.processName, r.info.applicationInfo.uid, true ); getLaunchTimeTracker().setLaunchTime(r); if (app != null && app.thread != null ) { try { ... realStartActivityLocked(r, app, andResume, checkConfig); return ; } catch (RemoteException e) { ... } } mService.startProcessLocked(r.processName, r.info.applicationInfo, true , 0 , "activity" , r.intent.getComponent(), false , false , true ); }
startSpecificActivityLocked 中会判断要跳转的 activity 所在的进程是否已经存在,如果存在,则调用 realStartActivityLocked ,如果没有存在,则调用 mService.startProcessLocked 创建进程。
先看看应用进程不存在的情况。之后的调用栈如下:1 2 3 4 -> ActivityManagerService.startProcessLocked -> ...startProcessLocked -> ...startProcess -> Process.start
1 2 3 4 5 -> Process.start -> zygoteProcess.start -> ...startViaZygote -> ...zygoteSendArgsAndGetResult -> ...openZygoteSocketIfNeeded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 private ZygoteState openZygoteSocketIfNeeded (String abi) throws ZygoteStartFailedEx { Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held" ); if (primaryZygoteState == null || primaryZygoteState.isClosed()) { try { primaryZygoteState = ZygoteState.connect(mSocket); } catch (IOException ioe) { throw new ZygoteStartFailedEx("Error connecting to primary zygote" , ioe); } maybeSetApiBlacklistExemptions(primaryZygoteState, false ); maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); } if (primaryZygoteState.matches(abi)) { return primaryZygoteState; } if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { try { secondaryZygoteState = ZygoteState.connect(mSecondarySocket); } catch (IOException ioe) { throw new ZygoteStartFailedEx("Error connecting to secondary zygote" , ioe); } maybeSetApiBlacklistExemptions(secondaryZygoteState, false ); maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); } if (secondaryZygoteState.matches(abi)) { return secondaryZygoteState; } throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); }
。。。这里没太看懂,但是网上的文章说 “其最终调用了Zygote并通过socket通信的方式让Zygote进程fork出一个新的进程,并根据传递的”android.app.ActivityThread”字符串,反射出该对象并执行ActivityThread的main方法对其进行初始化。“
。。。不知道他是怎么看出来的。。记个 TODO 吧
启动 activity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 final boolean realStartActivityLocked (ActivityRecord r, ProcessRecord app, boolean andResume, boolean checkConfig) throws RemoteException { ... final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread, r.appToken); clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent), System.identityHashCode(r), r.info, mergedConfiguration.getGlobalConfiguration(), mergedConfiguration.getOverrideConfiguration(), r.compat, r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results, newIntents, mService.isNextTransitionForward(), profilerInfo)); final ActivityLifecycleItem lifecycleItem; if (andResume) { lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward()); } else { lifecycleItem = PauseActivityItem.obtain(); } clientTransaction.setLifecycleStateRequest(lifecycleItem); mService.getLifecycleManager().scheduleTransaction(clientTransaction); ... return true ; }
这里核心是创建了一个 ClientTransaction ,先添加了一个 LaunchActivityItem 作为 callback , ClientTransaction 在调度是会先执行 callback 。然后设置 LifecycleStateRequest ,这里传入的 andResume == true ,所以设置的是 ResumeActivityItem 。
因为在之前 ClientTransaction 的调度流程已经分析过了,我们直接先来看 LaunchActivityItem.execute
activity 的 onCreate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Override public void execute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, mPendingResults, mPendingNewIntents, mIsForward, mProfilerInfo, client); client.handleLaunchActivity(r, pendingActions, null ); } public Activity handleLaunchActivity (ActivityClientRecord r, PendingTransactionActions pendingActions, Intent customIntent) { ... final Activity a = performLaunchActivity(r, customIntent); ... return a; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 private Activity performLaunchActivity (ActivityClientRecord r, Intent customIntent) { ComponentName component = r.intent.getComponent(); if (component == null ) { component = r.intent.resolveActivity( mInitialApplication.getPackageManager()); r.intent.setComponent(component); } if (r.activityInfo.targetActivity != null ) { component = new ComponentName(r.activityInfo.packageName, r.activityInfo.targetActivity); } ContextImpl appContext = createBaseContextForActivity(r); Activity activity = null ; try { java.lang.ClassLoader cl = appContext.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); StrictMode.incrementExpectedActivityCount(activity.getClass()); r.intent.setExtrasClassLoader(cl); r.intent.prepareToEnterProcess(); if (r.state != null ) { r.state.setClassLoader(cl); } } catch (Exception e) { ... } try { Application app = r.packageInfo.makeApplication(false , mInstrumentation); if (activity != null ) { ... appContext.setOuterContext(activity); activity.attach(appContext, this , getInstrumentation(), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config, r.referrer, r.voiceInteractor, window, r.configCallback); if (customIntent != null ) { activity.mIntent = customIntent; } r.lastNonConfigurationInstances = null ; checkAndBlockForNetworkAccess(); activity.mStartedActivity = false ; int theme = r.activityInfo.getThemeResource(); if (theme != 0 ) { activity.setTheme(theme); } activity.mCalled = false ; if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } if (!activity.mCalled) { throw new SuperNotCalledException( "Activity " + r.intent.getComponent().toShortString() + " did not call through to super.onCreate()" ); } r.activity = activity; } r.setState(ON_CREATE); mActivities.put(r.token, r); } catch (SuperNotCalledException e) { throw e; } catch (Exception e) { ... } return activity; } public Activity newActivity (ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException { String pkg = intent != null && intent.getComponent() != null ? intent.getComponent().getPackageName() : null ; return getFactory(pkg).instantiateActivity(cl, className, intent); }
在 performLaunchActivity 中创建了 activity ,并将 context 和 application 与之绑定,执行 activity.attach
。 接下来,performLaunchActivity 里通过调用 mInstrumentation.callActivityOnCreate
,开始进行 onCreate 的流程。
1 2 3 4 5 6 public void callActivityOnCreate (Activity activity, Bundle icicle) { prePerformCreate(activity); activity.performCreate(icicle); postPerformCreate(activity); }
activity 的 onStart 前面我们走完了 activity 的 onCreate 过程,执行代码是在 LaunchActivityItem 中,然后 onResume 显然是在 ResumeActivityItem 里, 那 onStart 呢??
再回到 ClientTransaction 的调度流程中来, 在 TransactionExecutor 里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 public void execute (ClientTransaction transaction) { final IBinder token = transaction.getActivityToken(); executeCallbacks(transaction); executeLifecycleState(transaction); mPendingActions.clear(); } private void executeLifecycleState (ClientTransaction transaction) { final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest(); if (lifecycleItem == null ) { return ; } final IBinder token = transaction.getActivityToken(); final ActivityClientRecord r = mTransactionHandler.getActivityClient(token); if (r == null ) { return ; } cycleToPath(r, lifecycleItem.getTargetState(), true ); lifecycleItem.execute(mTransactionHandler, token, mPendingActions); lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions); } private void cycleToPath (ActivityClientRecord r, int finish, boolean excludeLastState) { final int start = r.getLifecycleState(); final IntArray path = mHelper.getLifecyclePath(start, finish, excludeLastState); performLifecycleSequence(r, path); } private void performLifecycleSequence (ActivityClientRecord r, IntArray path) { final int size = path.size(); for (int i = 0 , state; i < size; i++) { state = path.get(i); switch (state) { ... case ON_START: mTransactionHandler.handleStartActivity(r, mPendingActions); break ; ... } } }
在 TransactionExecutor 中先执行 callback ,然后 lifecycleItem.execute 。中间有一个 cycleToPath 方法。 cycleToPath 内部逻辑很明显,执行 start 和 finish 中间状态的逻辑。 对于 ResumeActivityItem 来说就会执行 onStart 的逻辑。。。 我佛了。
activity 之后的 start 过程和 create 过程基本一致。现在再来看看 onResume
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Override public void execute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { client.handleResumeActivity(token, true , mIsForward, "RESUME_ACTIVITY" ); } @Override public void handleResumeActivity (IBinder token, boolean finalStateRequest, boolean isForward, String reason) { ... final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason); ... final Activity a = r.activity; ... ... Looper.myQueue().addIdleHandler(new Idler()); }
handleResumeActivity 中操作了很多 window 相关的东西。 到这里 activity 的启动就完成了
activity 的 stop 和 destroy 之前分析了 onPause 的流程, 但是没有看到 onStop , 所以 onStop 是如何执行的呢。 onStop的执行有两种case:
activity a 启动 activity b , a.onStop 应该在 a.onPause b.onResume 之后执行。
activity b 退出销毁,b.onStop 应该在 b.onPause 和 a.onResume 之后
来直接看执行puase的地方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class PauseActivityItem extends ActivityLifecycleItem { @Override public void execute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions, "PAUSE_ACTIVITY_ITEM" ); } @Override public int getTargetState () { return ON_PAUSE; } @Override public void postExecute (ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { ActivityManager.getService().activityPaused(token); } }
poseExecute 的执行在 execute 之后; pause完后会通知给ams。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 @Override public final void activityPaused (IBinder token) { synchronized (this ) { ActivityStack stack = ActivityRecord.getStackLocked(token); if (stack != null ) { stack.activityPausedLocked(token, false ); } } } final void activityPausedLocked (IBinder token, boolean timeout) { final ActivityRecord r = isInStackLocked(token); if (r != null ) { mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r); if (mPausingActivity == r) { mService.mWindowManager.deferSurfaceLayout(); try { completePauseLocked(true , null ); } finally { mService.mWindowManager.continueSurfaceLayout(); } return ; } else { if (r.isState(PAUSING)) { r.setState(PAUSED, "activityPausedLocked" ); if (r.finishing) { finishCurrentActivityLocked(r, FINISH_AFTER_VISIBLE, false , "activityPausedLocked" ); } } } } mStackSupervisor.ensureActivitiesVisibleLocked(null , 0 , !PRESERVE_WINDOWS); } private void completePauseLocked (boolean resumeNext, ActivityRecord resuming) { ActivityRecord prev = mPausingActivity; if (prev != null ) { prev.setWillCloseOrEnterPip(false ); final boolean wasStopping = prev.isState(STOPPING); prev.setState(PAUSED, "completePausedLocked" ); if (prev.finishing) { prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE, false , "completedPausedLocked" ); } else if (prev.app != null ) { if (mStackSupervisor.mActivitiesWaitingForVisibleActivity.remove(prev)) { if (prev.deferRelaunchUntilPaused) { prev.relaunchActivityLocked(false ,prev.preserveWindowOnDeferredRelaunch); } else if (wasStopping) { prev.setState(STOPPING, "completePausedLocked" ); } else if (!prev.visible || shouldSleepOrShutDownActivities()) { prev.setDeferHidingClient(false ); addToStopping(prev, true , false ); } } else { prev = null ; } ... mPausingActivity = null ; } } } final ActivityRecord finishCurrentActivityLocked (ActivityRecord r, int mode, boolean oomAdj, String reason) { final ActivityRecord next = mStackSupervisor.topRunningActivityLocked(true ); if (mode == FINISH_AFTER_VISIBLE && (r.visible || r.nowVisible) && next != null && !next.nowVisible) { if (!mStackSupervisor.mStoppingActivities.contains(r)) { addToStopping(r, false , false ); } r.setState(STOPPING, "finishCurrentActivityLocked" ); ... return r; } ... if (mode == FINISH_IMMEDIATELY || (prevState == PAUSED && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode())) || finishingActivityInNonFocusedStack || prevState == STOPPING || prevState == STOPPED || prevState == ActivityState.INITIALIZING) { r.makeFinishingLocked(); boolean activityRemoved = destroyActivityLocked(r, true , "finish-imm:" + reason); ... return activityRemoved ? null : r; } ... }
上面的代码中,activity在pause之后,无论是finish还是非finish的流程。 都不会立刻执行 stop 和 destroy 开始执行 stop 和 destroy ,是在下一个 activity resume 之后 来看 resume 之后做了啥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Override public void handleResumeActivity (IBinder token, boolean finalStateRequest, boolean isForward, String reason) { ... final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason); ... Looper.myQueue().addIdleHandler(new Idler()); } private class Idler implements MessageQueue .IdleHandler { @Override public final boolean queueIdle () { ... if (a.activity != null && !a.activity.mFinished) { try { am.activityIdle(a.token, a.createdConfig, stopProfiling); a.createdConfig = null ; } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } ... return false ; } }
在activity#resume 之后,会发送一个 idleHandler , 在消息队列空闲时调用 ams#activityIdle 开始上一个activity的销毁流程。 因为是一个 idleHandler ,只会在空闲时执行,所以,所以有可能一个页面退出后,很长时间都不执行 stop 和 destroy。 但是系统有一个10s的兜底,10s后一定会执行 stop 和 destroy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public final void activityIdle (IBinder token, Configuration config, boolean stopProfiling) { final long origId = Binder.clearCallingIdentity(); synchronized (this ) { ActivityStack stack = ActivityRecord.getStackLocked(token); if (stack != null ) { ActivityRecord r = mStackSupervisor.activityIdleInternalLocked(token, false , false , config); if (stopProfiling) { if ((mProfileProc == r.app) && mProfilerInfo != null ) { clearProfilerLocked(); } } } } Binder.restoreCallingIdentity(origId); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 final ActivityRecord activityIdleInternalLocked (final IBinder token, boolean fromTimeout, boolean processPausingActivities, Configuration config) { ... final ArrayList<ActivityRecord> stops = processStoppingActivitiesLocked(r, true , processPausingActivities); ... for (int i = 0 ; i < NS; i++) { r = stops.get(i); final ActivityStack stack = r.getStack(); if (stack != null ) { if (r.finishing) { stack.finishCurrentActivityLocked(r, ActivityStack.FINISH_IMMEDIATELY, false , "activityIdleInternalLocked" ); } else { stack.stopActivityLocked(r); } } } ... return r; } final ActivityRecord finishCurrentActivityLocked (ActivityRecord r, int mode, boolean oomAdj, String reason) { ... if (mode == FINISH_IMMEDIATELY || (prevState == PAUSED && (mode == FINISH_AFTER_PAUSE || inPinnedWindowingMode())) || finishingActivityInNonFocusedStack || prevState == STOPPING || prevState == STOPPED || prevState == ActivityState.INITIALIZING) { r.makeFinishingLocked(); boolean activityRemoved = destroyActivityLocked(r, true , "finish-imm:" + reason); .... return activityRemoved ? null : r; } ... } final boolean destroyActivityLocked (ActivityRecord r, boolean removeFromApp, String reason) { final boolean hadApp = r.app != null ; ... if (hadApp) { ... try { mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken, DestroyActivityItem.obtain(r.finishing, r.configChangeFlags)); } catch (Exception e) { ... } } ... return removedFromHistory; } final void stopActivityLocked (ActivityRecord r) { ... mService.getLifecycleManager().scheduleTransaction(r.app.thread, r.appToken, StopActivityItem.obtain(r.visible, r.configChangeFlags)); ... }
先判断 r.finishing
如果为true,表示页面退出,执行destroy; 如果为false,则只执行stop。 经过一番调用,最终还是一样,向 ClientLifecycleManager 发送了一个 StopActivityItem 和 DestroyActivityItem。
stop 和 destroy 系统10s兜底 在 ActivityRecord.completeResumeLocked
会在activity resume 之后调用,在这个方法中会调用ActivityStackSuperVisor.scheduleIdleTimeoutLocked()
1 2 3 4 5 6 7 8 9 void scheduleIdleTimeoutLocked (ActivityRecord next) { Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG, next); mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT); }
anr的判定 参考 (Android 9.0)Activity启动流程源码分析 Activity启动过程全解析 炼狱难度!腾讯Android高级岗:为什么 Activity.finish() 之后 10s 才 onDestroy ?