c、startPausingLocked 方法分析
startPausingLocked 方法中 , 调用了 mService.getLifecycleManager().scheduleTransaction 方法 , 该方法用于控制 Activity 声明周期的方法 , 其中涉及 PauseActivityItem 参数 , 说明要执行的是 Activity 的 onPause 生命周期方法 ;
/** * Start pausing the currently resumed activity. It is an error to call this if there * is already an activity being paused or there is no resumed activity. * * @param userLeaving True if this should result in an onUserLeaving to the current activity. * @param uiSleeping True if this is happening with the user interface going to sleep (the * screen turning off). * @param resuming The activity we are currently trying to resume or null if this is not being * called as part of resuming the top activity, so we shouldn't try to instigate * a resume here if not null. * @param pauseImmediately True if the caller does not want to wait for the activity callback to * complete pausing. * @return Returns true if an activity now is in the PAUSING state, and we are waiting for * it to tell us when it is done. */ final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, ActivityRecord resuming, boolean pauseImmediately) { mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken, PauseActivityItem.obtain(prev.finishing, userLeaving, prev.configChangeFlags, pauseImmediately)); }
注意 PauseActivityItem 专门用于处理 Activity Pause 生命周期 ;
/frameworks/base/core/java/android/app/servertransaction/PauseActivityItem.java
PauseActivityItem 继承了 ActivityLifecycleItem ,
/frameworks/base/core/java/android/app/servertransaction/ActivityLifecycleItem.java
ActivityLifecycleItem 继承了 ClientTransactionItem , ClientTransactionItem 会被 TransactionExecutor 执行 ,
/frameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java
执行 PauseActivityItem 后 , 会自动调用 ActivityThread 主线程的 handlePauseActivity 方法 ;
/frameworks/base/core/java/android/app/ActivityThread.java
PauseActivityItem 的 execute 方法就是上述控制 onResume 生命周期函数触发的方法 ;
第一个参数 ClientTransactionHandler client , ActivityThread 继承了 ClientTransactionHandler 接口 , 这里的第一个参数是 ActivityThread 主线程 ;
此处调用的 ActivityThread 的 handlePauseActivity 方法 ;
public class PauseActivityItem extends ActivityLifecycleItem {
private static final String TAG = "PauseActivityItem"; private boolean mFinished; private boolean mUserLeaving; private int mConfigChanges; private boolean mDontReport; @Override public void execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityPause"); client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions, "PAUSE_ACTIVITY_ITEM"); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); } }
d、继续回到 resumeTopActivityInnerLocked 方法分析
在 ActivityStack 中的 resumeTopActivityInnerLocked 方法中调用了 startPausingLocked 方法 , 其作用是触发上一个 Activity 的 Resume 生命周期 ;
在该 resumeTopActivityInnerLocked 方法的最后 , 调用了 ActivityStackSupervisor mStackSupervisor 成员的 startSpecificActivityLocked 方法 ;
class ActivityStack<T extends StackWindowController> extends ConfigurationContainer implements StackWindowListener { /** Run all ActivityStacks through this */ protected final ActivityStackSupervisor mStackSupervisor; @GuardedBy("mService") private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) { if (mResumedActivity != null) { if (DEBUG_STATES) Slog.d(TAG_STATES, "resumeTopActivityLocked: Pausing " + mResumedActivity); pausing |= startPausingLocked(userLeaving, false, next, false); } mStackSupervisor.startSpecificActivityLocked(next, true, false); } }
/frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
4、后续 ActivityStackSupervisor 源码分析
在 ActivityStackSupervisor 中的 startSpecificActivityLocked 方法中 , 调用的 realStartActivityLocked 方法是核心方法 , 在 realStartActivityLocked 方法中 , 获取到 LaunchActivityItem 后 , 传递给 clientTransaction , 去执行启动 Activity 的逻辑 ;
LaunchActivityItem 是要启动的目标 Activity ;
void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) { realStartActivityLocked(r, app, andResume, checkConfig); } final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app, boolean andResume, boolean checkConfig) throws RemoteException { // Create activity launch transaction. final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread, r.appToken); 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, app.repProcState, r.icicle, r.persistentState, results, newIntents, mService.isNextTransitionForward(), profilerInfo)); }
/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
LaunchActivityItem 继承了 ClientTransactionItem , ClientTransactionItem 是 ActivityThread ;
/frameworks/base/core/java/android/app/servertransaction/LaunchActivityItem.java
在 LaunchActivityItem 中的 execute 方法 , 就是调用 ClientTransactionHandler client 参数的 handleLaunchActivity 方法 ;
其中 ClientTransactionHandler client 参数就是 ActivityThread ;
public class LaunchActivityItem extends ClientTransactionItem { @Override public void execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions) { Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo, mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState, mPendingResults, mPendingNewIntents, mIsForward, mProfilerInfo, client); client.handleLaunchActivity(r, pendingActions, null /* customIntent */); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); } }
/frameworks/base/core/java/android/app/servertransaction/LaunchActivityItem.java
二、ActivityThread 主进程相关源码
在 ActivityThread 的 handleLaunchActivity 方法中 , 在该方法中调用了 performLaunchActivity 方法 ;
/** * Extended implementation of activity launch. Used when server requests a launch or relaunch. */ @Override public Activity handleLaunchActivity(ActivityClientRecord r, PendingTransactionActions pendingActions, Intent customIntent) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); mSomeActivitiesChanged = true; if (r.profilerInfo != null) { mProfiler.setProfiler(r.profilerInfo); mProfiler.startProfiling(); } // Make sure we are running with the most recent config. handleConfigurationChanged(null, null); if (localLOGV) Slog.v( TAG, "Handling launch of " + r); // Initialize before creating the activity if (!ThreadedRenderer.sRendererDisabled) { GraphicsEnvironment.earlyInitEGL(); } WindowManagerGlobal.initialize(); final Activity a = performLaunchActivity(r, customIntent); if (a != null) { r.createdConfig = new Configuration(mConfiguration); reportSizeConfigurations(r); if (!r.activity.mFinished && pendingActions != null) { pendingActions.setOldState(r.state); pendingActions.setRestoreInstanceState(true); pendingActions.setCallOnPostCreate(true); } } else { // If there was an error, for any reason, tell the activity manager to stop us. try { ActivityManager.getService() .finishActivity(r.token, Activity.RESULT_CANCELED, null, Activity.DONT_FINISH_TASK_WITH_ACTIVITY); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } return a; }
/frameworks/base/core/java/android/app/ActivityThread.java
在 performLaunchActivity 方法中调用了 Instrumentation 的 newActivity 方法 , 创建了一个新的 Activity 实例 ;
public final class ActivityThread extends ClientTransactionHandler { /** Core implementation of activity launch. */ private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ActivityInfo aInfo = r.activityInfo; activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); } }