Moosphan / Android-Daily-Interview

:pushpin:每工作日更新一道 Android 面试题,小聚成河,大聚成江,共勉之~

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2019-12-04:谈谈你对Window和WindowManager的理解?

Moosphan opened this issue · comments

2019-12-04:谈谈你对Window和WindowManager的理解?

window就是一个窗体嘛,用来装与用户交互(展示)内容的容器
manager:管理窗体?

这个博客讲的还不错,我也是这么理解的https://blog.csdn.net/joejames/article/details/79700503

commented

Window:抽象类,窗体容器.创建DecorView.
PhoneWindow:Window实现类.
AppCompatDelegateImpl:AppCompatDeleGate的实现类.在构造方法中传入了Window.该类是Activity中方法的代理实现类.如:setContentView()...

WindowManager:接口类.同时实现了ViewManager.定义了大量Window的状态值
WindowManagerImpl:WindowManager的接口实现类.但具体的方法实现交给了WindowManagerGlobal.
WindowManagerGlobal:真正的WindowManager接口方法的处理类.如:创建ViewRootImpl等..

Window/WindowManager均在Activity的attach中完成

`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,
Window window, ActivityConfigCallback activityConfigCallback) {
attachBaseContext(context);
mFragments.attachHost(null /parent/);

    mWindow = new PhoneWindow(this, window, activityConfigCallback);
    mWindow.setWindowControllerCallback(this);
    mWindow.setCallback(this);
    mWindow.setOnWindowDismissedCallback(this);
    mWindow.getLayoutInflater().setPrivateFactory(this);
    if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
        mWindow.setSoftInputMode(info.softInputMode);
    }
    if (info.uiOptions != 0) {
        mWindow.setUiOptions(info.uiOptions);
    }
    mUiThread = Thread.currentThread();
   ......
    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();
    ......
}`

windowManager是APP和window通迅的接口 对 Window 的访问必须通过 WindowManager也可以理解为windowManager是用于管理window的一个接口
window是一个窗体 我们常见的Activity dialog Toast都对应着一个window

Window 和 WindowManager 是什么关系?
Widow 是个抽象类,在 Android 中所有的视图都是通过 Window 来呈现的,包括 Activity、Dialog、Toast,它们的视图实际上都是附加在 Window 上的。Window 的具体实现类是 PhoneWindow。而 WindowManager 是外界访问 Window 的入口,WindowManager 和 WindowManagerService 之间通过 IPC 进行通信,从而实现对 Window 的访问和操作。
Window 和 View 是什么关系?
Window 是 View 的承载者,而 View 是 Window 的体现者。两者之间通过 ViewRootImpl 建立联系。
怎么理解这句话呢?
Window 是 View 的承载者:Android 中的所有视图都是附加在 Window 上呈现出来的 。
View 是 Window 的体现者:因为 Window 是个抽象的概念,并不实际存在,View 才是 Window 存在的实体。
而 ViewRootImpl 是用来建立 Window 和 View 之间的联系的,是两者之间的纽带。
WindowManager 和 View 是什么关系?
WindowManager 是 View 的直接管理者,对 View 的添加、删除、更新操作都是通过 WindowManager 来完成的,对应于 WindowManager 的 addView、removeView、updateViewLayout 三个方法。