Android Activity startup process source code analysis

In the source code analysis of the Android Launcher startup application process , it starts from clicking the application icon startActivity()and continues until the method of Applicationand is called. The middle process is somewhat high-energy, and the high-energy part is the common place of the two blog posts, and there is no way to bypass it. This article mainly describes the process from to to . Students who want to trace the source suggest to read the source code analysis of the Android Launcher startup application process first , and then read this blog post. Stop being verbose, Here we go~MainActivityonCreate()Activity#onCreate()Activity#onResume()

Whether it is started explicitly or implicitly Activity, it will enter intoActivityThread#handleLaunchActivity()

源码位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleLaunchActivity()

    private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {  
        ......  
        // 加载activity,然后调用onCreate,onStart方法
        Activity a = performLaunchActivity(r, customIntent);  
        if (a != null) {
            // 调用onResume方法
            handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed);
            ...   
        }
        ... 
    }  

The comments are a bit of a spoiler, today's focus is all on this method. first lookperformLaunchActivity()

ActivityThread#performLaunchActivity()

    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ...
            Activity activity = null;
        try {
            java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
            activity = mInstrumentation.newActivity(
                    cl, component.getClassName(), r.intent);
            ...
            }
            ...
        try {
            Application app = r.packageInfo.makeApplication(false, mInstrumentation);
            if (activity != null) {
                ...
                activity.attach(...)
                ...
                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;
                }
                ...
        return activity;
    }

mInstrumentation.callActivityOnCreateThe function of this line of code is to call . The source code analysis of the process of starting the applicationActivity#onCreate() in the Android Launcher has been introduced, so it will not be repeated here. Looking directly , the name of this method is very attractive ~ follow up.activity.performStart()

Source code location: frameworks/base/core/java/android/app/Activity.java
Activity#performStart()

    final void performStart() {
        mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
        mFragments.noteStateNotSaved();
        mCalled = false;
        mFragments.execPendingActions();
        mInstrumentation.callActivityOnStart(this);
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onStart()");
        }
        mFragments.dispatchStart();
        mFragments.reportLoaderStart();
        mActivityTransitionState.enterReady(this);
    }

The truth is about to come out~, follow up.

Source code location: frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()

    public void callActivityOnStart(Activity activity) {
        activity.onStart();
    }

Haha~ I found another way Activity#onStart(). It's really easy ~
now back to ActivityThread#performLaunchActivity()the middle handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed), follow up.

源码位置:frameworks/base/core/java/android/app/ActivityThread.java
ActivityThread#handleResumeActivity()
ActivityThread#ActivityClientRecord()

   final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume) {
        ...
        ActivityClientRecord r = performResumeActivity(token, clearHide);
        ...
    }

    public final ActivityClientRecord performResumeActivity(IBinder token,
            boolean clearHide) {
        ...
        r.activity.performResume();
        ...
    }

There are various state effects in the middle, and there is nothing to say. Follow up directly.

Source code location: frameworks/base/core/java/android/app/Activity.java
Activity#performResume()

    final void performResume() {
        performRestart();
        ...
        mInstrumentation.callActivityOnResume(this);
        ...
    }

There are two branches here
1. The sum onResume()needs to be executed before the execution . For example , when I returned from the last time, it is not the focus here, but I will look at it a little. 2. Direct executiononRestart()onStart()Activity
onResume()

Branch 1:
Activity#performRestart()

    final void performRestart() {
        ...
        mInstrumentation.callActivityOnRestart(this);
        performStart();
    }

These two methods have been analyzed above and will not be repeated here.

Branch 2:
Source code location: frameworks/base/core/java/android/app/Instrumentation.java
Instrumentation#callActivityOnStart()

    public void callActivityOnResume(Activity activity) {
        activity.mResumed = true;
        activity.onResume();

        if (mActivityMonitors != null) {
            synchronized (mSync) {
                final int N = mActivityMonitors.size();
                for (int i=0; i<N; i++) {
                    final ActivityMonitor am = mActivityMonitors.get(i);
                    am.match(activity, activity, activity.getIntent());
                }
            }
        }
    }

call directly Activity#OnResume().

ended. This blog ended faster and easier to understand than any previous blog post. The premise of all this is to have read my original blog post Android Launcher to start the application process source code analysis .


For more Framework source code analysis, please move to the Framework source code analysis series [Contents]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325543858&siteId=291194637