Activiy啟動流程(基于Android 8.0)

啟動Activity的過程有兩種,一種是從桌面Launch進(jìn)程啟動,另外一種是Activity啟動另外一個(gè)Activity

一、前半部分啟動分析

查看Activity$startActivity源碼如下:

1. Activity$startActivity

  @Override
    public void startActivity(Intent intent) {
        this.startActivity(intent, null);
    }

    //繼續(xù)查看

    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        if (options != null) {
            startActivityForResult(intent, -1, options);
        } else {
            // Note we want to go through this call for compatibility with
            // applications that may have overridden the method.
            startActivityForResult(intent, -1);
        }
    }

2.Activity$startActivityForResult

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
            @Nullable Bundle options) {
        if (mParent == null) {
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);
            if (ar != null) {
            //mMainThread是一個(gè)ActivityThread對象,該對象初始化在當(dāng)前Activity的attach方法中,attach方法在performLaunchActivity()里面調(diào)用.
            //attach方法中還創(chuàng)建了PhoneWindow對象,phoneWindow調(diào)用setWindowManager創(chuàng)建了WindowMangerImpl,負(fù)責(zé)View的添加,刪除,更新。
            
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                // If this start is requesting a result, we can avoid making
                // the activity visible until the result is received.  Setting
                // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
                // activity hidden during this time, to avoid flickering.
                // This can only be done when a result is requested because
                // that guarantees we will get information back when the
                // activity is finished, no matter what happens to it.
                mStartedActivity = true;
            }

            cancelInputsAndStartExitTransition(options);
            // TODO Consider clearing/flushing other event sources and events for child windows.
        } else {
            if (options != null) {
                mParent.startActivityFromChild(this, intent, requestCode, options);
            } else {
                // Note we want to go through this method for compatibility with
                // existing applications that may have overridden it.
                mParent.startActivityFromChild(this, intent, requestCode);
            }
        }
    }

mMainThread.getApplicationThread()返回的是一個(gè)ApplicationThread對象,ApplicationThread是ActivityThread的內(nèi)部類。
查看ApplicationThread源碼如下:

public final class ActivityThread {
   
   final ApplicationThread mAppThread = new ApplicationThread();

   //...code

   private class ApplicationThread extends IApplicationThread.Stub {
   
       //...code
   }

}

ApplicationThread繼承了IApplicationThread.Stub,說明這里Android使用了AIDL技術(shù)實(shí)現(xiàn)進(jìn)程間通信。Stub繼承自Binder,ApplicationThread就是一個(gè)Binder對象?;氐絪tartActivityForResult 調(diào)用了Instrumentation$execStartActivity,接著看execStartActivity的代碼。

3.Instrumentation$execStartActivity

public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) {
        IApplicationThread whoThread = (IApplicationThread) contextThread;

        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess(who);
            int result = ActivityManager.getService()
                .startActivity(whoThread, who.getBasePackageName(), intent,
                        intent.resolveTypeIfNeeded(who.getContentResolver()),
                        token, target != null ? target.mEmbeddedID : null,
                        requestCode, 0, null, options);
            //異常檢查,檢查Activity是否在Mainifest.xml中聲明
            checkStartActivityResult(result, intent);
        } catch (RemoteException e) {
            throw new RuntimeException("Failure from system", e);
        }
        return null;
    }
     /** @hide */
    public static void checkStartActivityResult(int res, Object intent) {
        if (!ActivityManager.isStartResultFatalError(res)) {
            return;
        }

        switch (res) {
            case ActivityManager.START_INTENT_NOT_RESOLVED:
            case ActivityManager.START_CLASS_NOT_FOUND:
                if (intent instanceof Intent && ((Intent)intent).getComponent() != null)
                    throw new ActivityNotFoundException(
                            "Unable to find explicit activity class "
                            + ((Intent)intent).getComponent().toShortString()
                            + "; have you declared this activity in your AndroidManifest.xml?");
                throw new ActivityNotFoundException(
                        "No Activity found to handle " + intent);
            case ActivityManager.START_PERMISSION_DENIED:
                throw new SecurityException("Not allowed to start activity "
                        + intent);
            case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:
                throw new AndroidRuntimeException(
                        "FORWARD_RESULT_FLAG used while also requesting a result");
            case ActivityManager.START_NOT_ACTIVITY:
                throw new IllegalArgumentException(
                        "PendingIntent is not an activity");
            case ActivityManager.START_NOT_VOICE_COMPATIBLE:
                throw new SecurityException(
                        "Starting under voice control not allowed for: " + intent);
            case ActivityManager.START_VOICE_NOT_ACTIVE_SESSION:
                throw new IllegalStateException(
                        "Session calling startVoiceActivity does not match active session");
            case ActivityManager.START_VOICE_HIDDEN_SESSION:
                throw new IllegalStateException(
                        "Cannot start voice activity on a hidden session");
            case ActivityManager.START_ASSISTANT_NOT_ACTIVE_SESSION:
                throw new IllegalStateException(
                        "Session calling startAssistantActivity does not match active session");
            case ActivityManager.START_ASSISTANT_HIDDEN_SESSION:
                throw new IllegalStateException(
                        "Cannot start assistant activity on a hidden session");
            case ActivityManager.START_CANCELED:
                throw new AndroidRuntimeException("Activity could not be started for "
                        + intent);
            default:
                throw new AndroidRuntimeException("Unknown error code "
                        + res + " when starting " + intent);
        }
    }

ActivityManager.getService().startActivity(...),就是IActivityManager的代理對象調(diào)用了startActivity方法,通過Binder機(jī)制,它會使系統(tǒng)服務(wù)ActivityManagerService的startActivity方法被調(diào)用。因此,啟動Activity的操作就交給了系統(tǒng)服務(wù)ActivityManagerService來處理了。

二、啟動Activity的交給系統(tǒng)服務(wù)ActivityManagerService

    @Override
    public final int startActivity(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
                resultWho, requestCode, startFlags, profilerInfo, bOptions,
                UserHandle.getCallingUserId());
    }

    //繼續(xù)查看

    @Override
    public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
        enforceNotIsolatedCaller("startActivity");
        userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
                userId, false, ALLOW_FULL_ONLY, "startActivity", null);
        // TODO: Switch to user app stacks here.
        return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,
                resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
                profilerInfo, null, null, bOptions, false, userId, null, null,
                "startActivityAsUser");
    }

最終會調(diào)用如下代碼:

final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
            boolean andResume, boolean checkConfig) throws RemoteException {
    //...code

    app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
                    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, !andResume,
                    mService.isNextTransitionForward(), profilerInfo);

    //...code       
}

app.thread調(diào)用scheduleLaunchActivity方法,通過Binder機(jī)制,會使ApplicationThread$scheduleLaunchActivity方法被調(diào)用。

三、啟動Activity的操作,交給ApplicationThread來處理

1.ApplicationThread$scheduleLaunchActivity

查看ApplicationThread$scheduleLaunchActivity方法源碼如下:

@Override
        public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
                ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
                CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
                int procState, Bundle state, PersistableBundle persistentState,
                List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
                boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {

            updateProcessState(procState, false);

            ActivityClientRecord r = new ActivityClientRecord();

            r.token = token;
            r.ident = ident;
            r.intent = intent;
            r.referrer = referrer;
            r.voiceInteractor = voiceInteractor;
            r.activityInfo = info;
            r.compatInfo = compatInfo;
            r.state = state;
            r.persistentState = persistentState;

            r.pendingResults = pendingResults;
            r.pendingIntents = pendingNewIntents;

            r.startsNotResumed = notResumed;
            r.isForward = isForward;

            r.profilerInfo = profilerInfo;

            r.overrideConfig = overrideConfig;
            updatePendingConfiguration(curConfig);

            sendMessage(H.LAUNCH_ACTIVITY, r);
        }

通過 sendMessage(H.LAUNCH_ACTIVITY, r)和本地UI線程進(jìn)行通信,查看H$handleMessage源碼如下:

private class H extends Handler {

    //...code
    public void handleMessage(Message msg) {
            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
            switch (msg.what) {
                case LAUNCH_ACTIVITY: {
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
                    final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
                    r.packageInfo = getPackageInfoNoCheck(
                            r.activityInfo.applicationInfo, r.compatInfo);
                    handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                } break;
        
    //...code

}

2.ActivityThread$handleLaunchActivity

查看ActivityThread$handleLaunchActivity源碼如下:

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
   //...code

   Activity a = performLaunchActivity(r, customIntent);

   //...code

   if (a != null) {
       //...code
       handleResumeActivity(r.token, false, r.isForward,
                   !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
       //...code

   } 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();
           }
   }

}

performLaunchActivity創(chuàng)建Activity,如果Activity不為空,則調(diào)用handleResumeActivity(),執(zhí)行OnResume回調(diào)。

3.ActivityThread$performLaunchActivity

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
       ActivityInfo aInfo = r.activityInfo;
       if (r.packageInfo == null) {
           r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
                   Context.CONTEXT_INCLUDE_CODE);
       }
       //Component 啟動Activity的方式:  ComponentName chatActivity =new ComponentName("com.example", "com.example.ui.MainActivity");
       //Intent intent =new Intent(); intent.setComponent(chatActivity); startActivity(intent);


       //獲取要啟動的Activity的ComponentName對象:里面包含了包名,類名相關(guān)的信息;
       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);
       }

       //創(chuàng)建了Activity的上下文環(huán)境
       ContextImpl appContext = createBaseContextForActivity(r);
       Activity activity = null;
       
       //...code
       java.lang.ClassLoader cl = appContext.getClassLoader();
       activity = mInstrumentation.newActivity(
               cl, component.getClassName(), r.intent);
                   
       //創(chuàng)建Application對象
       Application app = r.packageInfo.makeApplication(false, mInstrumentation);

           //...code

       if (activity != null) {
               
       //...code
       //調(diào)用Activity$attach方法:初始化PhoneWindow,WindowManagerImpl ,context上下文等
               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);

               //...code
               //啟動Activity:調(diào)用Instrumentation$callActivityOnCreate方法;
               mInstrumentation.callActivityOnCreate(activity, r.state);

       //...code
        }
  
       return activity;
}

r.packageInfo為LoadAPK對象,調(diào)用了LoadAPK$makeApplication,查看源碼如下:

public Application makeApplication(boolean forceDefaultAppClass,
            Instrumentation instrumentation) {
        if (mApplication != null) {
            return mApplication;
        }

    Application app = null;

    java.lang.ClassLoader cl = getClassLoader();
    //...
    ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
    app = mActivityThread.mInstrumentation.newApplication(
                    cl, appClass, appContext);
    
    //...
    mApplication = app;
    //調(diào)用此方法會回調(diào)Application的onCreate方法
    instrumentation.callApplicationOnCreate(app);
    //...

    return app;
}

創(chuàng)建Application對象跟創(chuàng)建Activity一樣,都是通過類加載器ClassLoader來創(chuàng)建實(shí)例。如果Application已經(jīng)存在,那么返回當(dāng)前的Application,保證單個(gè)進(jìn)程內(nèi)Application的唯一,創(chuàng)建完Application后,調(diào)用Activity.attach方法,初始化PhoneWindow等數(shù)據(jù),最后再調(diào)用 Instrumentation.callActivityOnCreate啟動Activity,這個(gè)方法內(nèi)部又調(diào)用了Activity$performCreate方法,最后回調(diào)Activity的onCreate方法。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容