栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android Activity、window、View的关系

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android Activity、window、View的关系

1.三者的关系

Activity管理window,window管理view

 从上图可以看出Activity包含Window,Window包含View。

2.源码分析

Activity.java:

public class Activity extends ContextThemeWrapper implements LayoutInflater.Factory2, Window.Callback, KeyEvent.Callback,OnCreateContextMenuListener, ComponentCallbacks2, Window.OnWindowDismissedCallback, WindowControllerCallback, AutofillManager.AutofillClient {

     ....

    private Window mWindow;

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

  // Window类是个抽象类,实现类是PhoneWindow

        mWindow = new PhoneWindow(this, window, activityConfigCallback); 

      mWindow.setWindowControllerCallback(this);

        mWindow.setCallback(this);

        mWindow.setOnWindowDismiss edCallback(this);

        mWindow.getLayoutInflater().setPrivateFa ctory(this);

        ....        

    }

 ....

}

从Activity源码中可以看到,在attach方法中创建了Window的实现类PhoneWindow。

PhoneWindow.java:

public PhoneWindow(Context context, Window preservedWindow, ActivityConfigCallback activityConfigCallback) {

    this(context);

    // Only main activity windows use decor context, all the other windows depend on whatever context that was given to them.

    mUseDecorContext = true;

    if (preservedWindow != null) {

        mDecor = (DecorView) preservedWindow.getDecorView();

        mElevation = preservedWindow.getElevation();

        mLoadElevation = false;

        mForceDecorInstall = true;

        getAttributes().token = preservedWindow.getAttributes().token;

    }

    boolean forceResizable = Settings.Global.getInt(context.getContentResolver(),DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0;

    mSupportsPictureInPicture = forceResizable ||context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);

    mActivityConfigCallback = activityConfigCallback;

}

在PhoneWindow的构造方法中有获取DecorView。

因此,可以看出:Activity持有Window对象(实现类是PhoneWindow);PhoneWindow构造方法中获取DecorView。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937491.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号