Activity、Window的关系

首先需要带着目的去阅读源码
这次的目的是:Activity的显示,Window起了什么作用?

建议:一边跟着文章,一边跟着源码阅读,否则会晕

1、activityThread收到handler消息

启动一个activity,调用performLaunchActivity方法

2、调用activity的attach方法

3、在activity的attach方法

attach方法中为mWindow赋值
mWindow = new PhoneWindow(this, window, activityConfigCallback);
⚠️关键点:Activity和Window的桥梁建立,Activity持有了window对象。

4、开始activity的生命周期

在onCreate方法中,调用setContentView方法,调用getWindow的setContentView方法。getWindow返回的值就是上面创建的phoneWindow。

public void setContentView(@LayoutRes int layoutResID) {
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

5、phoneWindow.setContentView()

其中,先创建了decorView,然后创建我们熟
悉的 R.id.content,最后把传入的layoutResID放入content中,实现了view的创建。这时候,PhoneWindow就把view创建完成。

⚠️关键点:setContenView方法,PhoneWindow创建View。Window和View 的桥梁建立。

两个桥梁已经创建,window就是activity和view的中间节点,activity对view的控制都需要通过window。

6、ActivityThread.handleResumeActivity 调用目标activity的makeVisible方法

void makeVisible() {
    if (!mWindowAdded) {
        ViewManager wm = getWindowManager();
        wm.addView(mDecor, getWindow().getAttributes());
        mWindowAdded = true;
    }
    mDecor.setVisibility(View.VISIBLE);
}

7、getWindowManager()返回的是什么?

//Activity.class
public WindowManager getWindowManager() {
    return mWindowManager;
}

mWindowManager在哪里创建?
mWindowManager = mWindow.getWindowManager();
调用window的getWindowManager方法

//Window.class
public WindowManager getWindowManager() {
    return mWindowManager;
}

window的mWindowManager创建

//Window.class
mWindowManager=((WindowManagerImpl)wm).createLocalWindowManager(this);
//WindowManagerImpl.class
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
    return new WindowManagerImpl(mContext, parentWindow);
}

⚠️关键点:所以,activity中使用的getWindowManager就是WindowManagerImpl

WindowManagerImpl实现了WindowManager接口,WindowManager继承自ViewManager

//ViewManager 接口
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);

所以WindowManagerImpl的主要目的就是对View的管理。

8、现在就是WindowManagerImpl调用了addView方法,把decorView传入。

//WindowManagerImpl.class
@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
    applyDefaultToken(params);
    mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}

这里出现了mGlobal对象。

private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

这个mGlobal是个单例,WindowManagerImpl对view的操作都是通过WindowManagerGlobal来实现的。

9、WindowManagerGlobal的addView方法,传入了decorView

//WindowManagerGlobal.class
//addView方法

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);
}

addView方法中,把decorview保存起来,创建了rootViewImpl对象(root),然后调用setView方法。

10、ViewRootImpl的setView方法

res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
        getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
        mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
        mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);

调用mWindowSession对象的addToDisplay方法,从方法名中也能看出,是把window添加到显示屏上。
这里就需要大致找一下mWindowSession是什么?

11、mWindowSession是什么?

//ViewRootImpl.class
mWindowSession = WindowManagerGlobal.getWindowSession();

调用WindowManagerGlobal的getWindowSession方法

//WindowManagerGlobal.class
//getWindowSession()方法
sWindowSession = windowManager.openSession(
        new IWindowSessionCallback.Stub() {
            @Override
            public void onAnimatorScaleChanged(float scale) {
                ValueAnimator.setDurationScale(scale);
            }
        },
        imm.getClient(), imm.getInputContext());

sWindowSession是windowManager.openSession方法返回的。这样,就需要到windowManager中去寻找,windowManager的实现类就是WindowManagerService

//WindowManagerService.class
@Override
public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,
        IInputContext inputContext) {
    if (client == null) throw new IllegalArgumentException("null client");
    if (inputContext == null) throw new IllegalArgumentException(“null inputContext”);
    Session session = new Session(this, callback, client, inputContext);
    return session;
}

返回的是一个Session对象
我们回到第10步,其中调用了addToDisplay方法,用来添加window。

12、Session的addToDisplay方法

//Session.class
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
        int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets,
        Rect outStableInsets, Rect outOutsets,
        DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel) {
    return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outFrame,
            outContentInsets, outStableInsets, outOutsets, outDisplayCutout, outInputChannel);
}

最终调用mService.addWindow方法。而mService是什么?
final WindowManagerService mService;
mService就是WindowManagerService。这样,window就通过IPC的方式,交给了系统服务,进行显示。

总结:

Activity创建后,window干了这些事

1、直面用户的activity创建后,其内部创建了Window对象。
2、调用setContentView后,Window对象把我们自己的xml通过inflate方式生成了View,并添加到了decorView中。
3、都创建完成后,就需要显示了,触发了ActivityThread.handleResumeActivity方法,这时,就需要把刚才生成的window显示到屏幕上
4、window的一路传递,最终交给了WindowManagerService处理进行显示。

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