Activity啟動
startActivity()
||
||
V
startActivityForResult()
||
||
V
ActivityStackSupervisor # realStartActivityLocked(){
//...
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
System.identityHashCode(r), r.info,
new Configuration(mService.mConfiguration), r.compat,
app.repProcState, r.icicle, results, newIntents, !andResume,
mService.isNextTransitionForward(), profileFile, profileFd,
profileAutoStop);
//...
}
此處的app.thread
是IApplicationThread
帆阳,他的實現(xiàn)類是ActivityThread
的內(nèi)部類ApplicationThread
。
那么看ApplicationThread
中的shceduleLaunchActivity
@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);
}
看下sendMessage
做了什么
sendMessage()
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
if (DEBUG_MESSAGES) Slog.v(
TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
+ ": " + arg1 + " / " + obj);
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
msg.arg1 = arg1;
msg.arg2 = arg2;
if (async) {
msg.setAsynchronous(true);
}
mH.sendMessage(msg);
}
搜索mH
恩伺,發(fā)現(xiàn)是private class H extends Handler
鹤树。是ActivityThread
的內(nèi)部類铣焊,和主線程Looper
關(guān)聯(lián),那么mH
的handleMessage()
就是在主線程里面執(zhí)行了罕伯。
搜索H類里面的H.LAUNCH_ACTIVITY
曲伊,發(fā)現(xiàn)了:
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);//在這
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
break;
//...
}
}
handleLaunchActivity()
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
//...
//創(chuàng)建Application和Activity對象,并調(diào)用它們的生命周期
Activity a = performLaunchActivity(r, customIntent);
if(a!=null){
//Activity#onResume
handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed);
}
}
performLaunchActivity()
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent){
ClassLoader cl = r.packageInfo.getClassLoader();
//根據(jù)ClassLoader創(chuàng)建Activity對象
Activity activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
//創(chuàng)建Application對象并調(diào)用App#onCreate
Application app=r.packageInfo.makeApplication(false,mInstrumentation);
//創(chuàng)建PhoneWindow追他,WindowManager坟募。并和Activity關(guān)聯(lián)
activity.attach(...);
//Activity#onCreate()
mInstrumentation.callActivityOnCreate(activity, r.state);
//Activity#onStart()
activity.performStart();
//...
}
r.pakageInfo.makeApplication()
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) {
if (mApplication != null) {
return mApplication;
}
Application app = null;
java.lang.ClassLoader cl = getClassLoader();
app = mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext);
mActivityThread.mAllApplications.add(app);
instrumentation.callApplicationOnCreate(app);//調(diào)用App.onCreate()
activity.onAttach()
final void attach(...) {
//實例化PhoneWindow
mWindow = new PhoneWindow(this);
mWindow.setCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
//實例化WindowManagerImpl,并和mWindow建立關(guān)聯(lián)湿酸。
mWindow.setWindowManager((WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
mWindowManager = mWindow.getWindowManager();
}
看下是否在此處就new出一個WindowManager
婿屹,點進setWindowManager()
。
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
//...
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
點進createLocalWindowManager(this)
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
//的確推溃,在這里實例化了WindowManagerImpl
return new WindowManagerImpl(mDisplay, parentWindow);
}
handleResumeActivity()
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward,
boolean reallyResume) {
//Activity#onResume()
ActivityClientRecord r = performResumeActivity(token, clearHide);
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
//讓DecorView變的不可見
decor.setVisibility(View.INVISIBLE);
//WindowManager在Activity#onAttach中已經(jīng)被實例化了
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
//WindowManager#addView昂利,將DecorView添加到Window中。
wm.addView(decor, l);
}
現(xiàn)在Activity的onResume
都執(zhí)行完了,執(zhí)行到了WindowManager#addView
蜂奸。
WindowManager
WindowManager
接口繼承自ViewMananger
犁苏。ViewManager
意思是:view管理者
public interface ViewManager{
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
}
因此WindowManager
也具有管理View管理者的能力,他的實現(xiàn)類是WindowManagerImpl
扩所∥辏看看他的addView
在做什么。
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
mGlobal是WindowManagerGlobal祖屏,從命名方式和getInstance
看出助赞,很明顯的全局單例。
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
看下WindowManagerGlobal中的4個引用
public final class WindowManagerGlobal {
...
//放著所有的DecorView袁勺。(有的博客說放著所有的View雹食,但是WMG里面用了mViews.add()的地方只有一個,而且只會傳入DecorView期丰。因此我認(rèn)為是只放DecorView)
private final ArrayList<View> mViews = new ArrayList<View>();
//放著所有的ViewRootImpl
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
//放著所有的WindowManager.LayoutParams
private final ArrayList<WindowManager.LayoutParams> mParams =new ArrayList<WindowManager.LayoutParams>();
//放著所有正在被刪除的View群叶。
private final ArraySet<View> mDyingViews = new ArraySet<View>();
...
}
接著mGlobal.addView里面在做什么。
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
ViewRootImpl root;
//實例化了ViewRootImpl钝荡,調(diào)用他的構(gòu)造方法街立。
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
root.setView(view, wparams, panelParentView);//調(diào)用了ViewRootImpl#setView。跟進去看下
}
ViewRootImpl#setView
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
mView = view;
// Schedule the first layout -before- adding to the window
// manager, to make sure we do the relayout before receiving
// any other events from the system.
requestLayout();
//將該Window添加到屏幕埠通。
//mWindowSession實現(xiàn)了IWindowSession接口赎离,它是Session的客戶端Binder對象.
//addToDisplay是一次AIDL的跨進程通信,通知WindowManagerService添加IWindow
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
view.assignParent(this);
}
requestLayout()里面調(diào)用scheduleTraversals();然后調(diào)用Choreographer編舞者的內(nèi)部的一系列方法植阴,用Handler和Looper,把doTraversals()放到主線程去輪詢蟹瘾。然后調(diào)用的就是三大遍歷,測量掠手,布局憾朴,繪制。