Android弹窗组件工作机制之Dialog、DialogFragment(一)

889 阅读5分钟

前言

Android在DialogFragment推出后,就已经不推荐继续使用Dialog,可替换为DialogFragment,其实DialogFragment只不过是对增加一层看不到的Fragment,用于监听生命周期,在Activity退出的时候会自动回收Dialog弹窗

基础概念

  • Activity:活动。控制生命周期和处理事件,统筹视图的添加与显示,控制Window和View的交互
  • Window:窗口。在Android中是个虚拟的概念,不是View,是承载View的载体,具体实现是PhoneWindow,承载着DecorView
  • WindowManager:窗口管理者。管理Window视图的添加或移除等,具体实现是WindowManagerService(wms)
  • DecorView:窗口根视图。本身是FrameLayout,是Window上真正的根布局,其包含两部分,标题和内容
  • TitleView:标题。作为DecorView的子View,其Id为@android:id/content
  • ContentViews:内容。作为DecorView的子View,其Id为@android:id/content
  • ViewRoot:连接wms和DecorView的纽带,View的measure、layout、draw均通过ViewRoot来完成,具体实现是ViewRootImpl

Dialog

在平时中,简单的弹出Dialog只需要这句话

new Dialog(MainActivity.this).show();

一、Dialog的显示

1、Dialog

Dialog的构造方法有多个,但最后都会调用这个构造方法

Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
    if (createContextThemeWrapper) {
        if (themeResId == 0) {
            //如果没有主题,则使用默认主题
            final TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
            themeResId = outValue.resourceId;
        }
        //包裹主题的Context
        mContext = new ContextThemeWrapper(context, themeResId);
    } else {
        mContext = context;
    }
    //获取windowManager服务
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    //创建新的Window
    final Window w = new PhoneWindow(mContext);
    mWindow = w;
    //设置callback
    w.setCallback(this);
    w.setOnWindowDismissedCallback(this);
    w.setWindowManager(mWindowManager, null, null);
    w.setGravity(Gravity.CENTER);
 
    mListenersHandler = new ListenersHandler(this);
}

从Dialog的构造方法中可以看出,Dialog实质上是个Window,其显示和隐藏也是借助WindowManager去控制的

2、Dialog.show

public void show() {
    //如果之前已经show过后,就让视图显示即可
    if (mShowing) {        
        if (mDecor != null) {            
            if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {                
                mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);            
            }            
            mDecor.setVisibility(View.VISIBLE);        
        }        
        return;   
    }    
    mCanceled = false;    
    
    //如果没有create则会调用dispatchOncreate,该方法最终会调用dialog的onCreate方法
    if (!mCreated) {
        dispatchOnCreate(null);
    }
 
    //dialog的onstart回调
    onStart();
    //获取decorView
    mDecor = mWindow.getDecorView();
 
    //如果需要ActionBar,则创建出来
    if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
        final ApplicationInfo info = mContext.getApplicationInfo();
        mWindow.setDefaultIcon(info.icon);
        mWindow.setDefaultLogo(info.logo);
        mActionBar = new WindowDecorActionBar(this);
    }
 
    //window参数的设置
    WindowManager.LayoutParams l = mWindow.getAttributes();
    if ((l.softInputMode
            & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
        WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
        nl.copyFrom(l);
        nl.softInputMode |=
                WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
        l = nl;
    }
 
    try {
        //windowManager将decorView加入视图
        mWindowManager.addView(mDecor, l);
        mShowing = true;
 
        sendShowMessage();
    } finally {
    }
}

show()其实就是走Dialog的生命周期,然后做初始化工作,获取Window上的DecorView后,将DecorView添加到视图上,这里需要注意的是在show()之后才执行onCreate()

3、Dialog.dispatchOnCreate

void dispatchOnCreate(Bundle savedInstanceState) {
    if (!mCreated) {
        onCreate(savedInstanceState); //回调onCreate()
        mCreated = true;
    }
}

protected void onCreate(Bundle savedInstanceState) {
    //由开发者实现
}

Dialog的初始化其实就是让用户去初始化自己的视图,平时我们是这么写的

public class RxDialog extends Dialog {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置视图
        setContentView(mView);
    }
}

具体的逻辑还是回到**setContentView()**设置Dialog的视图

4、Dialog.setContentView

public void setContentView(View view) {
    //调用mWindow进行视图设置,mWindow实际上就是构造方法中的PhoneWindow
    mWindow.setContentView(view);
}

mWindow则是在构造方法创建的PhoneWindow

5、PhoneWindow.setContentView

@Override
public void setContentView(View view) {
    //默认MATCH_PARENT
    setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
    if (mContentParent == null) {  
        installDecor();  //创建应用程序窗口视图对象
    } else {  
        mContentParent.removeAllViews();  //重新设置应用程序窗口的视图
    }  
 
    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        view.setLayoutParams(params);
        final Scene newScene = new Scene(mContentParent, view);
        transitionTo(newScene);
    } else {
        mContentParent.addView(view, params); //将我们传递进来的view添加布局上
    }
    mContentParent.requestApplyInsets();
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
    
    ......
}

PhoneWindow.setContentView()不仅在Dialog中存在,在Activity的setContentView也是走到这里。mContentParent指的是依附于DecorView上的R.id.content中的view。到这里只是将Dialog设置的View加载到PhoneWindow的ContentView上,其实更主要的还是PhoneWindow添加到我们的手机屏幕上,代码回溯到show()mWindowManager.addView(mDecor, l)

6、WindowManagerImpl.addView

WindowManager本质上是对View进行管理,但是WindowManager显然依然是个接口,其具体实现是WindowManagerImpl,最后还是委托给WindowManagerGlobal实例mGlobal处理

@Override
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
    applyDefaultToken(params);
    //委托给mGlobal来进行实现
    mGlobal.addView(view, params, mDisplay, mParentWindow);
}

7、WindowManagerGlobal.addView

public void addView(View view, ViewGroup.LayoutParams params,
        Display display, Window parentWindow) {
    
    ......
    
    final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
    if (parentWindow != null) {
        parentWindow.adjustLayoutParamsForSubWindow(wparams);
    } else {
        // If there's no parent, then hardware acceleration for this view is
        // set from the application's hardware acceleration setting.
        final Context context = view.getContext();
        if (context != null
                && (context.getApplicationInfo().flags
                        & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
            wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
        }
    }
 
    ViewRootImpl root;
    View panelParentView = null;
 
    synchronized (mLock) {
        // Start watching for system property changes.
        if (mSystemPropertyUpdater == null) {
            mSystemPropertyUpdater = new Runnable() {
                @Override public void run() {
                    synchronized (mLock) {
                        for (int i = mRoots.size() - 1; i >= 0; --i) {
                            mRoots.get(i).loadSystemProperties();
                        }
                    }
                }
            };
            SystemProperties.addChangeCallback(mSystemPropertyUpdater);
        }
 
        int index = findViewLocked(view, false);
        if (index >= 0) {
            if (mDyingViews.contains(view)) {
                // Don't wait for MSG_DIE to make it's way through root's queue.
                mRoots.get(index).doDie();
            } else {
                throw new IllegalStateException("View " + view
                        + " has already been added to the window manager.");
            }
            // The previous removeView() had not completed executing. Now it has.
        }
 
        // If this is a panel window, then find the window it is being
        // attached to for future reference.
        if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
                wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
            final int count = mViews.size();
            for (int i = 0; i < count; i++) {
                if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
                    panelParentView = mViews.get(i);
                }
            }
        }
 
        //创建ViewRootImpl,ViewRootImpl是view和window中的连接纽带
        root = new ViewRootImpl(view.getContext(), display);
 
        view.setLayoutParams(wparams);
        
        //存储相关View的信息,会在Remove的时候移除,相当于缓存
        mViews.add(view);//mViews:存储的是所有Window对应的View,本质是个List
        mRoots.add(root);//mRoots:存储的是所有Window所对应的ViewRootImpl,本质是个List
        mParams.add(wparams);//mParams:存储所有window对应的布局参数,本质是个List
    }
 
    // do this last because it fires off messages to start doing things
    try {
        //最终由root去实现最终的视图显示
        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;
    }
}

将视图添加到窗口上的工作交给root.setView(),root就是ViewRootImpl

8、ViewRootImpl.setView

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    synchronized (this) {
        if (mView == null) {
            ......
            requestLayout(); //真正完成视图的异步刷新请求
            
            ......
            //这里调用了mWindowSession的addToDisplay方法,在WindowManagerService层通过IPC机制完成真正的window添加
            res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                    getHostVisibility(), mDisplay.getDisplayId(),
                    mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                    mAttachInfo.mOutsets, mInputChannel);
           
        }
    }
}

@Override
public void requestLayout() {
    if (!mHandlingLayoutInLayoutRequest) {
        checkThread();
        mLayoutRequested = true;
        scheduleTraversals(); //真正的去走Measure、Layout、Draw
    }
}

在添加view之前,会走requestLayout(),真正实现View绘制的三部曲Measure、Layout、Draw。mWindowSession类型是IWindowSession,它是个Binder对象,真正的实现类是Session,window的添加过程实际上是一次ipc的调用,最后在WindowManagerService层通过IPC机制去实现的

总结

在这读完这里源码后,我们知道Window是个相对虚拟的对象,真正的操作是对Window中的DecorView进行addView()操作,而且在addView()之前,会先走onCreate()、onStart()、setContentView()操作,而在setContentView()过程中,会经过ViewRootImpl对象进行setView,并且在ViewRootImpl对象中会实现View绘制的三步曲,Measure、Layout、Draw操作,最后再将绘制好的view通过IWindowSession的ipc调用添加到界面上

  1. Dialog本质上是个Window,具体是通过Window的DecorView进行显示的
  2. Dialog是在show()之后走的onCreate()、onStart()、setContentView()等回调