android啓動流程分析(二)

上一篇介紹了apk啓動的流程到了創建activity的創建這裏接着繼續分析,先上圖片


看高清圖請下載

這裏從handleLaunchActivity方法繼續分析,如不明白的可以參考上一篇的分析android啓動流程分析,這邊直接就上代碼了

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

        Activity a = performLaunchActivity(r, customIntent);//創建activity

        if (a != null) {
            r.createdConfig = new Configuration(mConfiguration);
            reportSizeConfigurations(r);
            Bundle oldState = r.state;
            handleResumeActivity(r.token, false, r.isForward,
                    !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
           ......
        } else {
            ......
        }
    }

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);//通過Instrumentation這個類創建Activity

            ......
        } catch (Exception e) {
            if (!mInstrumentation.onException(activity, e)) {
                throw new RuntimeException(
                    "Unable to instantiate activity " + component
                    + ": " + e.toString(), e);
            }
        }
                ......
                activity.attach(appContext, this, getInstrumentation(), r.token,//attach方法
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor, window);
                ......
                int theme = r.activityInfo.getThemeResource();//設置主題
                   if (theme != 0) {
                         activity.setTheme(theme);
                     }

                ......
                if (r.isPersistable()) {//根據本地是否持久化來判斷走那個方法,(這個持久化個人理解是activity被不正常銷燬造成的)
                                        //這個方法調用的是activity的oncreate方法
                    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
                } else {
                    mInstrumentation.callActivityOnCreate(activity, r.state);
                }
				......
				 if (!r.activity.mFinished) {
                    activity.performStart();//調用activity的onstart方法
                    r.stopped = false;
                }
                ......
                if (!r.activity.mFinished) {
                    if (r.isPersistable()) {
                        if (r.state != null || r.persistentState != null) {
                            mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
                                    r.persistentState);
                        }
                    } else if (r.state != null) {//r.state是AMS傳過來的,也就是說OnRestoreInstanceState的調用取決於AMS是否想恢復activity。
                        mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
                    }
                }
                if (!r.activity.mFinished) {
                    activity.mCalled = false;
                    if (r.isPersistable()) {
                        mInstrumentation.callActivityOnPostCreate(activity, r.state,
                                r.persistentState);
                    } else {
                        mInstrumentation.callActivityOnPostCreate(activity, r.state);
                    }
                    if (!activity.mCalled) {
                        throw new SuperNotCalledException(
                            "Activity " + r.intent.getComponent().toShortString() +
                            " did not call through to super.onPostCreate()");
                    }
                }
            }
            r.paused = true;

            mActivities.put(r.token, r);

        } catch (SuperNotCalledException e) {
            throw e;

        } catch (Exception e) {
            if (!mInstrumentation.onException(activity, e)) {
                throw new RuntimeException(
                    "Unable to start activity " + component
                    + ": " + e.toString(), e);
            }
        }

        return activity;
    }

通過上面的代碼可以看到activity再啓動的時候首選調用attach--->oncreate--->onStart--->onPostCreate

對於attach是activity的一些初始化配置,在這個方法中會創建PhoneWindow對象,對於onPostCreate方法是Activity完全起來之後調用的方法(官方解釋Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called),對於這個方法目前不知道有什麼特殊的用法如有特殊用法,希望能提出。其他的兩個方法這裏就不多說了我們常用的方法。上面還有個不是生命週期的一部分的是onRestoreInstanceState這個方法的調用是在AMS判定需要調用的時候纔會調用(轉屏,activity不正常被系統回收等)

    final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
        ......

        // TODO Push resumeArgs into the activity for consideration
        r = performResumeActivity(token, clearHide, reason);

         .....
                if (a.mVisibleFromClient && !a.mWindowAdded) {//將decor添加到Window上
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                }

         ....
    }
 public final ActivityClientRecord performResumeActivity(IBinder token,
            boolean clearHide, String reason) {
           ......
            try {
                r.activity.onStateNotSaved();
                r.activity.mFragments.noteStateNotSaved();
                if (r.pendingIntents != null) {
                    deliverNewIntents(r, r.pendingIntents);
                    r.pendingIntents = null;
                }
                if (r.pendingResults != null) {
                    deliverResults(r, r.pendingResults);
                    r.pendingResults = null;
                }
                r.activity.performResume();
				......
        }
        return r;
    }

  final void performResume() {
        performRestart();
		.....
        mInstrumentation.callActivityOnResume(this);//調用了onResume方法
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onResume()");
        }

        ......
        mFragments.dispatchResume();
        mFragments.execPendingActions();

        onPostResume();//
        if (!mCalled) {
            throw new SuperNotCalledException(
                "Activity " + mComponent.toShortString() +
                " did not call through to super.onPostResume()");
        }
    }

 final void performRestart() {
        mFragments.noteStateNotSaved();

        if (mToken != null && mParent == null) {
            // No need to check mStopped, the roots will check if they were actually stopped.
            WindowManagerGlobal.getInstance().setStoppedState(mToken, false /* stopped */);
        }

        if (mStopped) {//當時從stop狀態恢復的時候走下面的代碼
            mStopped = false;

            synchronized (mManagedCursors) {
                final int N = mManagedCursors.size();
                for (int i=0; i<N; i++) {
                    ManagedCursor mc = mManagedCursors.get(i);
                    if (mc.mReleased || mc.mUpdated) {
                        if (!mc.mCursor.requery()) {
                            if (getApplicationInfo().targetSdkVersion
                                    >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                                throw new IllegalStateException(
                                        "trying to requery an already closed cursor  "
                                        + mc.mCursor);
                            }
                        }
                        mc.mReleased = false;
                        mc.mUpdated = false;
                    }
                }
            }

            mCalled = false;
            mInstrumentation.callActivityOnRestart(this);
            if (!mCalled) {
                throw new SuperNotCalledException(
                    "Activity " + mComponent.toShortString() +
                    " did not call through to super.onRestart()");
            }
            performStart();
        }
    }
通過上面的方法可以找到activity接下來正常的運行聲明週期應該是-onResure---->onPostResure,但是如果要是從stop狀態下回復Activity的時候就會先調用onRestart--->onstart-->onResure--->onPostResure

到這裏activity就算啓動完成了,顯現出來了,上面的代碼分析是Activity啓動的聲明週期,下一篇分析Activity關閉的生命週期


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章