Android loading process analysis (from app startup to execution of onCreate)

The main entrance of Android software is also main, this main method is defined in ActivityThread:

public static void main(String[] args) {
    
    
    ...
    Looper.prepareMainLooper();

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

    if (sMainThreadHandler == null) {
    
    
        sMainThreadHandler = thread.getHandler();
    }
	...
        
    Looper.loop();
	...
}

The Handlermechanism is mainly used here . Since this article mainly talks about the Android loading mechanism, it is not too much analysis Handler. mainThere is mainly one method, which is Looperalways waiting for the message ( Message). mainMost of the methods are Handlersome operations of the main thread . The important thing is:

thread.attach(false);

View attachmethod

private void attach(boolean system) {
    
    
    ...
    final IActivityManager mgr = ActivityManager.getService(); //1
    try {
    
    
        mgr.attachApplication(mAppThread);  //2
    } catch (RemoteException ex) {
    
    
        throw ex.rethrowFromSystemServer();
    }
    ...
}

1 obtained at IActivityManageran object mgr, the object is a system through a Bindermechanism created a Activitymanagement class, called by the management system services, and by bindercall interfaces among.

The ApplicationThreadobject at 2 mAppThreadis ActivityThreada member variable, and the class is also ActivityThreadan internal class

private class ApplicationThread extends IApplicationThread.Stub {
    
    
    public final void scheduleResumeActivity(IBinder token, int processState,
    boolean isForward, Bundle resumeArgs) {
    
    
		...
        sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
    }

    // we use token to identify this activity without having to send the
    // activity itself back to the activity manager. (matters more with ipc)
    @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) {
    
    
		...
        sendMessage(H.LAUNCH_ACTIVITY, r);
    }
    ...
}

That mgroperating system (a system service call), whereas ApplicationThreadonly ActivityThreadthe inner class, so calling mgrthe attachApplicationrelationship so that android application to build the operating system, the operating system after receiving the corresponding event, by ApplicationThreadthe method of sending to the app System information . For example, when the user opens the app, the system will mgrindirectly call scheduleLaunchActivitythis method to Handlersend information to the main thread :

sendMessage(H.LAUNCH_ACTIVITY, r);

View Handlerof the handlerMessagemethod

public void handleMessage(Message msg) {
    
    
    ...
    switch (msg.what) {
    
    
        case LAUNCH_ACTIVITY: {
    
    
            ...
            final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
            handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
        } break;
        ...
    }
}

ActivityClientRecordThe object saves the parameters passed by the system, viewhandleLaunchActivity

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
    
    
    Activity a = performLaunchActivity(r, customIntent);
    if (a != null) {
    
    
    	...
        handleResumeActivity(r.token, false, r.isForward,
            !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
        ...
    }
    ...
}

This method mainly executes Activitythe load (Launch) or Resume work, continue to view performLaunchActivity:

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    
    
    ...
    try {
    
    
		...
        if (activity != null) {
    
    
            ...
            if (r.isPersistable()) {
    
    
                mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
            } else {
    
    
                mInstrumentation.callActivityOnCreate(activity, r.state);
            }
            ...
        }
    }
    return activity;     
}	

Ways to view mInstrumentationobjectscallActivityOnCreate

public void callActivityOnCreate(Activity activity, Bundle icicle,
	PersistableBundle persistentState) {
    
    
	prePerformCreate(activity);
	activity.performCreate(icicle, persistentState);
	postPerformCreate(activity);
}

From the above three methods name of view, which is mainly made Activityof Create, has Createfront and rear (pre and post) work, see performCreateMethods

final void performCreate(Bundle icicle, PersistableBundle persistentState) {
    
    
	...
    if (persistentState != null) {
    
    
        onCreate(icicle, persistentState);
    } else {
    
    
        onCreate(icicle);
    }
    ...
}

To which dawned, Handlerupon receipt of the corresponding information ( Messageexecutes the operation after), and finally calls Activitythe onCreatemethod, thus completing Activitythe Launchprocess.

Guess you like

Origin blog.csdn.net/weixin_48968045/article/details/114499320