Activity启动过程调用

1.入口 ActivityThread.main()

 ActivityThread thread = new ActivityThread();
 thread.attach(false);

2.Activity.attach()

final IActivityManager mgr = ActivityManagerNative.getDefault();
        try {
            mgr.attachApplication(mAppThread);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }

通过IBinder调用获取ActivityManager对象,把当前创建的ActivityThread与ActivityManager对象进行关联。

3.通过IBinder对象回调ActivityThread.scheduleLaunchActivity(),在这个方法中new ActivityClientRecord对象,并设置一堆参数,在给Handler发消息

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

4.ActivityThread.handlerMessage() 方法接收到消息会调用handleLaunchActivity()方法

handleLaunchActivity(r, null, "LAUNCH_ACTIVITY")`;

5.ActivityThread.handleLaunchActivity()方法会调用performLaunchActivity()方法获取到Activity对象,这里面是用反射获取Activity对象。

Activity a = performLaunchActivity(r, customIntent);

6.ActivityThread.performLaunchActivity()方法会调用activity.attach方法,在往下走调用了
mInstrumentation.callActivityOnCreate()方法,这样就走到mInstrumentation.callActivityOnCreate()方法,最后调用activity.performStart()方法;

     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);
                    
     if (r.isPersistable()) {
                mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
            } else {
                mInstrumentation.callActivityOnCreate(activity, r.state);
            }
            
    if (!r.activity.mFinished) {  
                activity.performStart();  
                r.stopped = false;  
            }  

这样 Activityattach(),onCreate(),onStart() 都执行了。这个方法执行完在返回 ActivityThread.handleLaunchActivity 中,
往下走会调用 handleResumeActivity() 方法,这个方法中调用 performResumeActivity(),这个方法中调用 r.activity.performResume(),从而调用到 Activity.onResume()

你可能感兴趣的:(Activity启动过程调用)