Activity 的组成

2021-06-08  本文已影响0人  ITRenj

Activity 系列博客

Activity 的组成

Activity 代表一个窗口,实际上这个窗口并不是 Activity,而是由 一个 继承至Window抽象类的 PhoneWindow 对象mWindow 来表示的,mWindowActivity 的成员变量,主要作用就是负责窗口的管理。之所以说是管理窗口的,是因为它并不能将界面展示出来,展现界面是由它管理的 DecorView 对象来完成, 在Activity中我们可以通过getWindow().getDecorView()方法来获取DecorView对象,DecorView 类是 FrameLayout 的子类, 也是整个 View 树的根布局(这里也能说明Android事件的分发是从ViewGroup开始的,如果需要了解更多关于Android事件分发问题,可以浏览《 Android中的事件分发机制》这篇博客)。DecorView 由三部分构成: ActionBar、标题区(标题区根据加载布局的不同,可能会没有)和内容区。

Activity 构成.png

对上图进行说明:

  1. 首先是最外层的 Activity,一般我们是继承自 AppCompatActivity,但是 AppCompatActivity 最终也是继承自 Activity 的,所以我们的最外框层,就是 Activity
  2. Activity相当于一个框,它包含了Window(实际上是Window的子类PhoneWindow)来管理窗口,Activity其他功能这里不做说明
  3. PhoneWindow管理窗口,它包含了DecorView用来渲染和显示内容(包括ActionBar、TitleBar和ContentParent部分)
  4. DecorViewFrameLayout的子类,也是整个视图的根节点,开发者写的布局layout.xml文件最终是被添加到DecorView的ContentParent部分
  5. 当在讨论Android事件的分发是从View还是ViewGroup开始时,从DecorView extends FrameLayout extends ViewGroup就能得到Android事件是从ViewGroup开始的(如果需要了解更多关于Android事件分发问题,可以浏览《 Android中的事件分发机制》这篇博客)

源码解析

注意:源码版本为 Android 10(Api 29),不同Android版本可能有一些差别

setContentView()执行过程

    // Activity 中的 setContentView() 方法
    public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

getWindow() 方法返回的是 Window 类型的成员变量 mWindow ,实际是 PhoneWindow 对象。(mWindow 的创建过程请继续往下看,在后面【Activity中PhoneWindow在什么时候创建的】中会说到),我们现在继续看 PhoneWindow 中的 setContentView() 方法:

@Override
public void setContentView(int layoutResID) {
    if (mContentParent == null) {
        installDecor(); // 内容根布局为null时,调用 installDecor() 方法创建 DecorView
    } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        mContentParent.removeAllViews();
    }

    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                getContext());
        transitionTo(newScene);
    } else {
        // 将传递的 布局文件id 通过 inflate 添加到 mContentParent 中
        mLayoutInflater.inflate(layoutResID, mContentParent); 
    }
    mContentParent.requestApplyInsets();
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
    mContentParentExplicitlySet = true;
}

通过以上我们知道了,Activity 中调用 setContentView() 方法实际上就是调用了 PhoneWindow 中的 setContentView() 方法,然后创建 DecorView 以及将布局内容增加到 DecorView 中过程。

DecorView 的创建和 布局内容填充的过程

在上面方法中,在初次的时候,mContentParent 肯定为 null,所以会走 installDecor() 方法创建 DecorView

private void installDecor() {
    mForceDecorInstall = false;
    if (mDecor == null) {
        mDecor = generateDecor(-1); // 关键点1:创建 DecorView
    } else {
        mDecor.setWindow(this);
    }
    if (mContentParent == null) {
        mContentParent = generateLayout(mDecor); // 关键点2:初始化内容区域 mContentParent
        mDecor.makeOptionalFitsSystemWindows();

        // 下面的代码是设置标题(如果需要并且存在TitleView)和Activity样式、效果等,忽略
    }
}

通过 new DecorView() 直接创建 DecorView 对象,看看 DecorView 的定义,继承至 FrameLayout

    public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks

以上代码中的资源文件位于 ...\sdk\platforms\android-29\data\res\layout 目录下(android-29表示对应的android版本),以下是 screen_title.xml 文件中的内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <!-- Popout bar for action modes -->
    <ViewStub android:id="@+id/action_mode_bar_stub"
              android:inflatedId="@+id/action_mode_bar"
              android:layout="@layout/action_mode_bar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="?attr/actionBarTheme" />
    <FrameLayout
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/windowTitleSize"
        style="?android:attr/windowTitleBackgroundStyle">
        <TextView android:id="@android:id/title" 
            style="?android:attr/windowTitleStyle"
            android:background="@null"
            android:fadingEdge="horizontal"
            android:gravity="center_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

其他不同的资源文件具体内容可以自行查看,最外层都是 LinearLayout 包裹的,也就就是为什么在上方图中,DecorView 中为什么先包含了LinearLayout 之后,在 LinearLayout 中包含 ActionView、TitleView、ContentParent 的原因。

当完成后,我们回头继续看 PhoneWindow 中的 setContentView() 方法中的 mLayoutInflater.inflate(layoutResID, mContentParent) 这行代码,这行代码的作用就是将自定义的界面布局加载到 mContentParent 中。具体的实现过程,可以通过《Android inflate解析》《Android inflate实例解析》 进行了解。

通过以上过程,我们就从源码方面知道了 Activity 的组成以及 setContentView() 方法的具体执行过程了。下面做一个简单的总结:

Activity中PhoneWindow在什么时候创建的

通过查看Activity的源码可以找到PhoneWindow的创建是在Activityattach()方法中(attach()方法在Activity中是onCreate()方法调用之前系统自动调用的一个方法),同时也获取到了WindowManager对象

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);
    ...
    // 创建PhoneWindow对象
    mWindow = new PhoneWindow(this);
    ... // 将各种参数赋给Activity的成员变量
    // 通过PhoneWindow对象获取mWindowManager对象
    mWindowManager = mWindow.getWindowManager();
    ...
}

Activty 系统回调 attach() 方法的过程可以通过以下博客了解:

上一篇 下一篇

猜你喜欢

热点阅读