前面文章介绍了 Activity 窗口创建的流程,这里我们在看一下 Dialog 和 Toast 窗口创建的流程。
Dialog 中创建 Window
是在其构造方法中完成。
源码位置:/frameworks/base/core/java/android/app/Dialog.java
- private final WindowManager mWindowManager;
-
- Dialog(@UiContext @NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
-
- ……
- // 获取 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.setOnWindowSwipeDismissedCallback(() -> {
- if (mCancelable) {
- cancel();
- }
- });
- w.setWindowManager(mWindowManager, null, null);
- w.setGravity(Gravity.CENTER);
-
- mListenersHandler = new ListenersHan