App應(yīng)用窗口由Activity加載,整個窗口創(chuàng)建和Activity啟動關(guān)聯(lián)在一起乳绕。
如圖所示
從Activity啟動說起
Activity啟動方法在ActivityThread的performLaunchActivity方法中,保留主要方法調(diào)用
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
............
ActivityInfo aInfo = r.activityInfo;
if (r.packageInfo == null) {
r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
Context.CONTEXT_INCLUDE_CODE);
}
ComponentName component = r.intent.getComponent();
if (component == null) {
component = r.intent.resolveActivity(
mInitialApplication.getPackageManager());
r.intent.setComponent(component);
}
if (r.activityInfo.targetActivity != null) {
component = new ComponentName(r.activityInfo.packageName,
r.activityInfo.targetActivity);
}
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
//通過classloader 加載Activity類
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
r.intent.prepareToEnterProcess();
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
if (localLOGV) Slog.v(
TAG, r + ": app=" + app
+ ", appName=" + app.getPackageName()
+ ", pkg=" + r.packageInfo.getPackageName()
+ ", comp=" + r.intent.getComponent().toShortString()
+ ", dir=" + r.packageInfo.getAppDir());
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Configuration config = new Configuration(mCompatConfiguration);
if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
+ r.activityInfo.name + " with config " + config);
//構(gòu)建activity內(nèi)部環(huán)境
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);
if (customIntent != null) {
activity.mIntent = customIntent;
}
r.lastNonConfigurationInstances = null;
activity.mStartedActivity = false;
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
//調(diào)用onCreate方法绞惦,里面調(diào)用setContentView
if (r.isPersistable()) {
mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
} else {
mInstrumentation.callActivityOnCreate(activity, r.state);
}
......................
return activity;
}
mInstrumentation.newActivity(..),通過ClassLoader實現(xiàn)Activity的實例化刷袍;
執(zhí)行activity.attach(..)方法翩隧,里面構(gòu)建activity實例所需要的內(nèi)部變量,包括創(chuàng)建Window和設(shè)置WindowManger
Window(PhoneWindow)和WindowManger創(chuàng)建
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
//創(chuàng)建PhoneWindow呻纹,用于視圖加載
mWindow = new PhoneWindow(this);
//將window事件通過回調(diào)接口,傳遞到Activity來處理
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
//設(shè)置WindowManger
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}
attach(..)方法中专缠,通過實例化創(chuàng)建PhoneWindow實例雷酪,通過context.getSystemService()方法獲取WindowManager,
DecorView 創(chuàng)建
到現(xiàn)在Window和WindowmWindowManger已經(jīng)創(chuàng)建好了涝婉,Window創(chuàng)建好哥力,需要添加視圖View,所有視圖的根視圖是DecorView,DecorView的創(chuàng)建在 setContent方法中吩跋。
上面說到創(chuàng)建Activity實例寞射,調(diào)用attch方法,接下來就是調(diào)用mInstrumentation.callActivityOnCreate(..)方法锌钮,后面會調(diào)用Activity.onCreate()方法桥温,在onCreate()方法中,通過setContentView()方法設(shè)置整個APP視圖梁丘。
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
setContent最終調(diào)用的是PhoneWindow的setContentView方法侵浸,
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
................
}
installDecor()方法調(diào)用generateDecor()方法來創(chuàng)建DecorView,DecorView是所有布局的根布局氛谜。
WindowManger何時會將Window添加進(jìn)來掏觉,閱讀后面代碼會發(fā)現(xiàn)當(dāng)Activity執(zhí)行OnResume時添加,添加時還會創(chuàng)建一個ViewRootImpl實例
ViewRootImpl創(chuàng)建
當(dāng)ActivityThread執(zhí)行handleResumeActivity方法時值漫,里面會執(zhí)行r.activity.makeVisible()方法
void makeVisible() {
if (!mWindowAdded) {
//將DecorView添加到WindowManager
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
addView在WindowManagerImpl實現(xiàn)中調(diào)用WindowManagerGlobal的addView方法
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
//ViewRootImpl 創(chuàng)建
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
try {
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
synchronized (mLock) {
final int index = findViewLocked(view, false);
if (index >= 0) {
removeViewLocked(index, true);
}
}
throw e;
}
}
到此DecorView展示出來澳腹,顯示在屏幕中,wm.addView(...)創(chuàng)建了ViewRootImpl杨何,ViewRootImpl負(fù)責(zé)繪制各個子view
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
synchronized (this) {
.........
requestLayout();
.................
//通過IPC遵湖,添加window交給WindowMangerServer
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
.........
}
}
mWindowSession類型是IWindowSession,是一個Binder對象晚吞,實現(xiàn)類是Seession延旧,addToDisplay方法通過IPC調(diào)用WindowManagerService的addWindow(...)方法來添加Window;
requestLayout()方法調(diào)用scheduleTraversals()方法,后面異步方法刷新請求
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
if (!mUnbufferedInputDispatch) {
scheduleConsumeBatchedInput();
}
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
}
在TraversalRunnable的run方法里面調(diào)用doTraversal()方法槽地,doTraversal()方法會執(zhí)行performTraversals()方法迁沫,后面就是view的三個流程
void doTraversal() {
if (mTraversalScheduled) {
mTraversalScheduled = false;
mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
if (mProfile) {
Debug.startMethodTracing("ViewAncestor");
}
performTraversals();
if (mProfile) {
Debug.stopMethodTracing();
mProfile = false;
}
}
}
ViewRootImpl除了繪制view,還包括分發(fā)底層傳遞的行為事件捌蚊。如通過WindowInputEventReceiver獲取傳遞過來的點擊事件集畅,通過handler轉(zhuǎn)發(fā)一直傳遞到Activity,大致流程:
硬件 --> ViewRootImpl --> DecorView --> PhoneWindow --> Activity
Activity不負(fù)責(zé)視圖控制缅糟,一個Activity包含一個Window挺智,Windo表示一個窗口
WIndow是視圖承載器,內(nèi)部持有DecorView窗宦,DecorView是view的根布局赦颇;Window是抽象類,真正持有DecorView是PhoneWindow赴涵。Window通過WindowManger將DecorView添加進(jìn)來媒怯,將DecorView交給ViewRoot進(jìn)行繪制和其他交互操作。
DecorView是Fragment子類髓窜,為Android視圖樹的根節(jié)點扇苞。
ViewRootImpl通過IWindow連接WindowmangerService和DecorView欺殿。View繪制流程也是ViewRoot完成。ViewRoot不屬于View樹一部分鳖敷,可以接收事件分發(fā)脖苏,以及界面刷新等
Activity類似控制器,不負(fù)責(zé)視圖部分定踱。Window是承載器棍潘,裝在視圖;DecorView是頂層視圖屋吨,ViewRootImpl連接器蜒谤,負(fù)責(zé)事件傳遞和用戶交互。