1.ActivityThread

  • main()
    ActivityThread Thread = new ActivityThread(); ActivityThread = new ActivityThread(); thread.attach(false);Copy the code
  • attach()App communicates with system processes across processes through Binder
    RuntimeInit.setApplicationObject(mAppThread.asBinder());
    final IActivityManager mgr = ActivityManager.getService();
    try {
         mgr.attachApplication(mAppThread, startSeq);
    } catch (RemoteException ex) {
         throw ex.rethrowFromSystemServer();
    }
    Copy the code
  • ApplicationThread A collection of information processing methods called by AMS
    public final void bindApplication(...) {... // Focus on the message sendMessage(h.bin_application, data) to be processed by the Handler; }Copy the code
  • H inherits from handler
    • handleMessage()
    public void handleMessage(Message msg) { ... switch (msg.what) { ... case BIND_APPLICATION: ... handleBindApplication(data); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; . case EXECUTE_TRANSACTION: final ClientTransaction transaction = (ClientTransaction) msg.obj; mTransactionExecutor.execute(transaction); if (isSystem()) { // Client transactions inside system process are recycled on the client side // instead of ClientLifecycleManager to avoid being cleared before this // message is handled. transaction.recycle(); } // TODO(lifecycler): Recycle locally scheduled transactions. break; . }}Copy the code
    • handleBindApplication()Application is instantiated here
    private void handleBindApplication(AppBindData data) { ... / / reflection to create an Application object app = data. Info. MakeApplication (data restrictedBackupMode, null); . / / call Application lifecycle try {mInstrumentation. OnCreate (data. InstrumentationArgs); } catch (Exception e) { ... } try { mInstrumentation.callApplicationOnCreate(app); } catch (Exception e) { ... }... }Copy the code
    • The ActivityThread extends ClientTransactionHandler is covered later
  • handleLaunchActivity()Act will be used later
    /** * Extended implementation of activity launch. Used when server requests a launch or relaunch. */ @Override public Activity handleLaunchActivity(ActivityClientRecord r, PendingTransactionActions pendingActions, Intent customIntent) { ... 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(); }}... }Copy the code
  • performLaunchActivity()Reflection create act
    /** Core implementation of activity launch. */ private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ... 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) { if (! mInstrumentation.onException(activity, e)) { throw new RuntimeException( "Unable to instantiate activity " + component + ": " + e.toString(), e); }}... Window window = null; if (r.mPendingRemoveWindow ! = null && r.mPreserveWindow) { window = r.mPendingRemoveWindow; r.mPendingRemoveWindow = null; r.mPendingRemoveWindowManager = null; } appContext.setOuterContext(activity); * Attach (appContext, this, getInstrumentation(), r.toy, R.i.dent, app, R.I.ntent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config, r.referrer, r.voiceInteractor, window, r.configCallback); . / / start the life cycle of the if (r.i sPersistable ()) {mInstrumentation. CallActivityOnCreate (activity, r.s Tate, r.p ersistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); }... }Copy the code

2.ActivityManager

  • Communication with AMS is established through Binder mechanisms
    private static final Singleton<IActivityManager> IActivityManagerSingleton = new Singleton<IActivityManager>() { @Override protected IActivityManager create() { final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE); final IActivityManager am = IActivityManager.Stub.asInterface(b); return am; }}; . /** * @hide */ public static IActivityManager getService() { return IActivityManagerSingleton.get(); }Copy the code

3.ActivityManagerService

  • attachApplication()
    @Override public final void attachApplication(IApplicationThread thread, long startSeq) { synchronized (this) { int callingPid = Binder.getCallingPid(); final int callingUid = Binder.getCallingUid(); final long origId = Binder.clearCallingIdentity(); attachApplicationLocked(thread, callingPid, callingUid, startSeq); Binder.restoreCallingIdentity(origId); }}Copy the code
  • attachApplicationLocked()
    private final boolean attachApplicationLocked(IApplicationThread thread, int pid, int callingUid, long startSeq) { ... thread.bindApplication(processName, appInfo, providers, app.instr.mClass, profilerInfo, app.instr.mArguments, app.instr.mWatcher, app.instr.mUiAutomationConnection, testMode, mBinderTransactionTrackingEnabled, enableTrackAllocation, isRestrictedBackupMode || ! normalMode, app.persistent, new Configuration(getGlobalConfiguration()), app.compat, getCommonServicesLocked(app.isolated), mCoreSettingsObserver.getCoreSettingsLocked(), buildSerial, isAutofillCompatEnabled); . }Copy the code

4.LoadedApk

  • makeApplication()
    public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) { ... / / retrieve application calssName there is to get the name of the custom application If no reference system default String appClass = mApplicationInfo. ClassName. if (forceDefaultAppClass || (appClass == null)) { appClass = "android.app.Application"; } try {// Use calssLoader to create application java.lang.ClassLoader cl = getClassLoader(); if (! mPackageName.equals("android")) { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "initializeJavaContextClassLoader"); initializeJavaContextClassLoader(); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } / / create applicationContexxt ContextImpl appContext = ContextImpl. CreateAppContext (mActivityThread, this); app = mActivityThread.mInstrumentation.newApplication( cl, appClass, appContext); appContext.setOuterContext(app); } catch (Exception e) { ... }... // Note ~~~ important !!!!! mStackSupervisor.attachApplicationLocked(app); . }Copy the code

5. After the Application to create, mStackSupervisor. AttachApplicationLocked (app)

  • ActivityStackSupervisor.attachApplicationLocked()
    boolean attachApplicationLocked(ProcessRecord app) throws RemoteException { ... try { if (realStartActivityLocked(activity, app, top == activity /* andResume */, true /* checkConfig */)) { didSomething = true; } } catch (RemoteException e) { Slog.w(TAG, "Exception in new application when starting activity " + top.intent.getComponent().flattenToShortString(), e); throw e; }... }Copy the code
  • ActivityStackSupervisor.realStartActivityLocked
    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)); // Set desired final state. final ActivityLifecycleItem lifecycleItem; if (andResume) { lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward()); } else { lifecycleItem = PauseActivityItem.obtain(); } clientTransaction.setLifecycleStateRequest(lifecycleItem); // Schedule transaction. mService.getLifecycleManager().scheduleTransaction(clientTransaction); . }Copy the code
  • ClientLifecycleManager.scheduleTransaction()
     void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
          final IApplicationThread client = transaction.getClient();
          transaction.schedule();
          if (!(client instanceof Binder)) {
              // If client is not an instance of Binder - it's a remote call and at this point it is
              // safe to recycle the object. All objects used for local calls will be recycled after
              // the transaction is executed on client in ActivityThread.
              transaction.recycle();
          }
      }
    Copy the code
  • ClientTransaction.schedule()
    /** * Schedule the transaction after it was initialized. It will be send to client and all its * individual parts will be applied in the following sequence: * 1. The client calls {@link #preExecute(ClientTransactionHandler)}, which triggers all work * that needs to be done before actually scheduling the transaction for callbacks and * lifecycle  state request. * 2. The transaction message is scheduled. * 3. The client calls {@link TransactionExecutor#execute(ClientTransaction)}, which executes * all callbacks and necessary lifecycle transitions. */ public void schedule() throws RemoteException { . / / IApplicationThread scheduleTransaction mClient scheduleTransaction (this); }Copy the code
  • ActivityThread.ApplicationThread.scheduleTransaction()
    @Override
          public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
              ActivityThread.this.scheduleTransaction(transaction);
          }
    Copy the code
  • ClientTransactionHandler.scheduleTransaction()Handler sends messages to ActivityThread.H for processing
    /** Prepare and schedule transaction for execution. */
      void scheduleTransaction(ClientTransaction transaction) {
          transaction.preExecute(this);
          sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
      }
    Copy the code
  • TransactionExecutor
    • .execute()
    /** * Resolve transaction. * First all callbacks will be executed in the order they appear in the list. If a callback * requires a certain pre- or post-execution state, the client will be transitioned accordingly. * Then the client will cycle to the final lifecycle state if provided. Otherwise, it will * either remain in the initial state, or last state needed by a callback. */ public void execute(ClientTransaction transaction) { if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Start resolving transaction"); final IBinder token = transaction.getActivityToken(); . executeCallbacks(transaction); executeLifecycleState(transaction); mPendingActions.clear(); if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "End resolving transaction"); }Copy the code
    • ExecuteCallbacks () wake up android. Intent. The category. The LAUNCHER
    /** Cycle through all states requested by callbacks and execute them at proper times. */ @VisibleForTesting public void executeCallbacks(ClientTransaction transaction) { final List<ClientTransactionItem> callbacks = transaction.getCallbacks(); if (callbacks == null || callbacks.isEmpty()) { // No callbacks to execute, return early. return; } if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Resolving callbacks in transaction"); final IBinder token = transaction.getActivityToken(); ActivityClientRecord r = mTransactionHandler.getActivityClient(token); // In case when post-execution state of the last callback matches the final state requested // for the activity in this transaction, we won't do the last transition here and do it when // moving to final state instead (because it may contain additional parameters from server). final ActivityLifecycleItem finalStateRequest = transaction.getLifecycleStateRequest(); final int finalState = finalStateRequest ! = null ? finalStateRequest.getTargetState() : UNDEFINED; // Index of the last callback that requests some post-execution state. final int lastCallbackRequestingState = lastCallbackRequestingState(transaction); final int size = callbacks.size(); for (int i = 0; i < size; ++i) { final ClientTransactionItem item = callbacks.get(i); if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Resolving callback: " + item); final int postExecutionState = item.getPostExecutionState(); final int closestPreExecutionState = mHelper.getClosestPreExecutionState(r, item.getPostExecutionState()); if (closestPreExecutionState ! = UNDEFINED) { cycleToPath(r, closestPreExecutionState, transaction); Execute (mTransactionHandler, Token, mPendingActions);} // Execute item.execute(mTransactionHandler, token, mPendingActions); item.postExecute(mTransactionHandler, token, mPendingActions); if (r == null) { // Launch activity request will create an activity record. r = mTransactionHandler.getActivityClient(token); } if (postExecutionState ! = UNDEFINED && r ! = null) { // Skip the very last transition and perform it by explicit state request instead. final boolean shouldExcludeLastTransition = i == lastCallbackRequestingState && finalState == postExecutionState; cycleToPath(r, postExecutionState, shouldExcludeLastTransition, transaction); }}}Copy the code
    • ExecuteLifecycleState () act.create Subsequent lifecycle calls
        private void executeLifecycleState(ClientTransaction transaction) {
        ...
          // Cycle to the state right before the final requested state.
          cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */);
      
          // Execute the final transition with proper parameters.
          lifecycleItem.execute(mTransactionHandler, token, mPendingActions);
          lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);     
        }
      Copy the code
    • .cycleToPath()
       /**
       * Transition the client between states with an option not to perform the last hop in the
       * sequence. This is used when resolving lifecycle state request, when the last transition must
       * be performed with some specific parameters.
       */
      private void cycleToPath(ActivityClientRecord r, int finish,
              boolean excludeLastState) {
          final int start = r.getLifecycleState();
          log("Cycle from: " + start + " to: " + finish + " excludeLastState:" + excludeLastState);
          final IntArray path = mHelper.getLifecyclePath(start, finish, excludeLastState);
          performLifecycleSequence(r, path);
      }
      Copy the code
    • .performLifecycleSequence()
      /** Transition the client through previously initialized state sequence. */ private void performLifecycleSequence(ActivityClientRecord r, IntArray path) { final int size = path.size(); for (int i = 0, state; i < size; i++) { state = path.get(i); log("Transitioning to state: " + state); 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); }}}Copy the code
  • LaunchActivityItem.execute()Callback to ActivityThread. HandleLaunchActivity start act
     @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);
      }
    Copy the code

5.* Activity

  • attach()Style act, register context, window, etc
    final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, NonConfigurationInstances lastNonConfigurationInstances, Configuration config, String referrer, IVoiceInteractor voiceInteractor, Window window, ActivityConfigCallback activityConfigCallback) { attachBaseContext(context); // Assign global context mfragments. attachHost(null /*parent*/); MWindow = new PhoneWindow(this, Window, activityConfigCallback); mWindow.setWindowControllerCallback(this); mWindow.setCallback(this); mWindow.setOnWindowDismissedCallback(this); mWindow.getLayoutInflater().setPrivateFactory(this); if (info.softInputMode ! = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { mWindow.setSoftInputMode(info.softInputMode); } if (info.uiOptions ! = 0) { mWindow.setUiOptions(info.uiOptions); }... mWindow.setWindowManager( (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, mComponent.flattenToString(), (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) ! = 0); if (mParent ! = null) { mWindow.setContainer(mParent.getWindow()); } mWindowManager = mWindow.getWindowManager(); mCurrentConfig = config; mWindow.setColorMode(info.colorMode); setAutofillCompatibilityEnabled(application.isAutofillCompatibilityEnabled()); enableAutofillCompatibilityIfNeeded(); }Copy the code
  • performCreate()
    final void performCreate(Bundle icicle, PersistableBundle persistentState) { mCanEnterPictureInPicture = true; restoreHasCurrentPermissionRequest(icicle); //-->oncreate if (persistentState ! = null) { onCreate(icicle, persistentState); } else { onCreate(icicle); } writeEventLog(LOG_AM_ON_CREATE_CALLED, "performCreate"); mActivityTransitionState.readState(icicle); mVisibleFromClient = ! mWindow.getWindowStyle().getBoolean( com.android.internal.R.styleable.Window_windowNoDisplay, false); //-->fragment mFragments.dispatchActivityCreated(); mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions()); }Copy the code

5.** Instrumentation

  • Instrumentation.callActivityOnCreate()

    public void callActivityOnCreate(Activity activity, Bundle icicle) {
       prePerformCreate(activity);
       activity.performCreate(icicle);
       postPerformCreate(activity);
    }
    Copy the code