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

Myth丶恋晨 2023-06-24 04:49 43阅读 0赞

前言

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只需要这句话

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

一、Dialog的显示

1、Dialog

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

  1. Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
  2. if (createContextThemeWrapper) {
  3. if (themeResId == 0) {
  4. //如果没有主题,则使用默认主题
  5. final TypedValue outValue = new TypedValue();
  6. context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
  7. themeResId = outValue.resourceId;
  8. }
  9. //包裹主题的Context
  10. mContext = new ContextThemeWrapper(context, themeResId);
  11. } else {
  12. mContext = context;
  13. }
  14. //获取windowManager服务
  15. mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  16. //创建新的Window
  17. final Window w = new PhoneWindow(mContext);
  18. mWindow = w;
  19. //设置callback
  20. w.setCallback(this);
  21. w.setOnWindowDismissedCallback(this);
  22. w.setWindowManager(mWindowManager, null, null);
  23. w.setGravity(Gravity.CENTER);
  24. mListenersHandler = new ListenersHandler(this);
  25. }

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

2、Dialog.show

  1. public void show() {
  2. //如果之前已经show过后,就让视图显示即可
  3. if (mShowing) {
  4. if (mDecor != null) {
  5. if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
  6. mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);
  7. }
  8. mDecor.setVisibility(View.VISIBLE);
  9. }
  10. return;
  11. }
  12. mCanceled = false;
  13. //如果没有create则会调用dispatchOncreate,该方法最终会调用dialog的onCreate方法
  14. if (!mCreated) {
  15. dispatchOnCreate(null);
  16. }
  17. //dialog的onstart回调
  18. onStart();
  19. //获取decorView
  20. mDecor = mWindow.getDecorView();
  21. //如果需要ActionBar,则创建出来
  22. if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
  23. final ApplicationInfo info = mContext.getApplicationInfo();
  24. mWindow.setDefaultIcon(info.icon);
  25. mWindow.setDefaultLogo(info.logo);
  26. mActionBar = new WindowDecorActionBar(this);
  27. }
  28. //window参数的设置
  29. WindowManager.LayoutParams l = mWindow.getAttributes();
  30. if ((l.softInputMode
  31. & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
  32. WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
  33. nl.copyFrom(l);
  34. nl.softInputMode |=
  35. WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
  36. l = nl;
  37. }
  38. try {
  39. //windowManager将decorView加入视图
  40. mWindowManager.addView(mDecor, l);
  41. mShowing = true;
  42. sendShowMessage();
  43. } finally {
  44. }
  45. }

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

3、Dialog.dispatchOnCreate

  1. void dispatchOnCreate(Bundle savedInstanceState) {
  2. if (!mCreated) {
  3. onCreate(savedInstanceState); //回调onCreate()
  4. mCreated = true;
  5. }
  6. }
  7. protected void onCreate(Bundle savedInstanceState) {
  8. //由开发者实现
  9. }
  10. 1
  11. 2
  12. 3
  13. 4
  14. 5
  15. 6
  16. 7
  17. 8
  18. 9
  19. 10

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

  1. public class RxDialog extends Dialog {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. //设置视图
  6. setContentView(mView);
  7. }
  8. }
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8

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

4、Dialog.setContentView

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

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

5、PhoneWindow.setContentView

  1. @Override
  2. public void setContentView(View view) {
  3. //默认MATCH_PARENT
  4. setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  5. }
  6. @Override
  7. public void setContentView(View view, ViewGroup.LayoutParams params) {
  8. if (mContentParent == null) {
  9. installDecor(); //创建应用程序窗口视图对象
  10. } else {
  11. mContentParent.removeAllViews(); //重新设置应用程序窗口的视图
  12. }
  13. if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  14. view.setLayoutParams(params);
  15. final Scene newScene = new Scene(mContentParent, view);
  16. transitionTo(newScene);
  17. } else {
  18. mContentParent.addView(view, params); //将我们传递进来的view添加布局上
  19. }
  20. mContentParent.requestApplyInsets();
  21. final Callback cb = getCallback();
  22. if (cb != null && !isDestroyed()) {
  23. cb.onContentChanged();
  24. }
  25. ......
  26. }
  27. 1
  28. 2
  29. 3
  30. 4
  31. 5
  32. 6
  33. 7
  34. 8
  35. 9
  36. 10
  37. 11
  38. 12
  39. 13
  40. 14
  41. 15
  42. 16
  43. 17
  44. 18
  45. 19
  46. 20
  47. 21
  48. 22
  49. 23
  50. 24
  51. 25
  52. 26
  53. 27
  54. 28
  55. 29

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处理

  1. @Override
  2. public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
  3. applyDefaultToken(params);
  4. //委托给mGlobal来进行实现
  5. mGlobal.addView(view, params, mDisplay, mParentWindow);
  6. }
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6

7、WindowManagerGlobal.addView

  1. public void addView(View view, ViewGroup.LayoutParams params,
  2. Display display, Window parentWindow) {
  3. ......
  4. final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
  5. if (parentWindow != null) {
  6. parentWindow.adjustLayoutParamsForSubWindow(wparams);
  7. } else {
  8. // If there's no parent, then hardware acceleration for this view is
  9. // set from the application's hardware acceleration setting.
  10. final Context context = view.getContext();
  11. if (context != null
  12. && (context.getApplicationInfo().flags
  13. & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
  14. wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
  15. }
  16. }
  17. ViewRootImpl root;
  18. View panelParentView = null;
  19. synchronized (mLock) {
  20. // Start watching for system property changes.
  21. if (mSystemPropertyUpdater == null) {
  22. mSystemPropertyUpdater = new Runnable() {
  23. @Override public void run() {
  24. synchronized (mLock) {
  25. for (int i = mRoots.size() - 1; i >= 0; --i) {
  26. mRoots.get(i).loadSystemProperties();
  27. }
  28. }
  29. }
  30. };
  31. SystemProperties.addChangeCallback(mSystemPropertyUpdater);
  32. }
  33. int index = findViewLocked(view, false);
  34. if (index >= 0) {
  35. if (mDyingViews.contains(view)) {
  36. // Don't wait for MSG_DIE to make it's way through root's queue.
  37. mRoots.get(index).doDie();
  38. } else {
  39. throw new IllegalStateException("View " + view
  40. + " has already been added to the window manager.");
  41. }
  42. // The previous removeView() had not completed executing. Now it has.
  43. }
  44. // If this is a panel window, then find the window it is being
  45. // attached to for future reference.
  46. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
  47. wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
  48. final int count = mViews.size();
  49. for (int i = 0; i < count; i++) {
  50. if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
  51. panelParentView = mViews.get(i);
  52. }
  53. }
  54. }
  55. //创建ViewRootImpl,ViewRootImpl是view和window中的连接纽带
  56. root = new ViewRootImpl(view.getContext(), display);
  57. view.setLayoutParams(wparams);
  58. //存储相关View的信息,会在Remove的时候移除,相当于缓存
  59. mViews.add(view);//mViews:存储的是所有Window对应的View,本质是个List
  60. mRoots.add(root);//mRoots:存储的是所有Window所对应的ViewRootImpl,本质是个List
  61. mParams.add(wparams);//mParams:存储所有window对应的布局参数,本质是个List
  62. }
  63. // do this last because it fires off messages to start doing things
  64. try {
  65. //最终由root去实现最终的视图显示
  66. root.setView(view, wparams, panelParentView);
  67. } catch (RuntimeException e) {
  68. // BadTokenException or InvalidDisplayException, clean up.
  69. synchronized (mLock) {
  70. final int index = findViewLocked(view, false);
  71. if (index >= 0) {
  72. removeViewLocked(index, true);
  73. }
  74. }
  75. throw e;
  76. }
  77. }
  78. 1
  79. 2
  80. 3
  81. 4
  82. 5
  83. 6
  84. 7
  85. 8
  86. 9
  87. 10
  88. 11
  89. 12
  90. 13
  91. 14
  92. 15
  93. 16
  94. 17
  95. 18
  96. 19
  97. 20
  98. 21
  99. 22
  100. 23
  101. 24
  102. 25
  103. 26
  104. 27
  105. 28
  106. 29
  107. 30
  108. 31
  109. 32
  110. 33
  111. 34
  112. 35
  113. 36
  114. 37
  115. 38
  116. 39
  117. 40
  118. 41
  119. 42
  120. 43
  121. 44
  122. 45
  123. 46
  124. 47
  125. 48
  126. 49
  127. 50
  128. 51
  129. 52
  130. 53
  131. 54
  132. 55
  133. 56
  134. 57
  135. 58
  136. 59
  137. 60
  138. 61
  139. 62
  140. 63
  141. 64
  142. 65
  143. 66
  144. 67
  145. 68
  146. 69
  147. 70
  148. 71
  149. 72
  150. 73
  151. 74
  152. 75
  153. 76
  154. 77
  155. 78
  156. 79
  157. 80
  158. 81
  159. 82
  160. 83
  161. 84
  162. 85
  163. 86
  164. 87

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

8、ViewRootImpl.setView

  1. public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
  2. synchronized (this) {
  3. if (mView == null) {
  4. ......
  5. requestLayout(); //真正完成视图的异步刷新请求
  6. ......
  7. //这里调用了mWindowSession的addToDisplay方法,在WindowManagerService层通过IPC机制完成真正的window添加
  8. res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
  9. getHostVisibility(), mDisplay.getDisplayId(),
  10. mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
  11. mAttachInfo.mOutsets, mInputChannel);
  12. }
  13. }
  14. }
  15. @Override
  16. public void requestLayout() {
  17. if (!mHandlingLayoutInLayoutRequest) {
  18. checkThread();
  19. mLayoutRequested = true;
  20. scheduleTraversals(); //真正的去走Measure、Layout、Draw
  21. }
  22. }
  23. 1
  24. 2
  25. 3
  26. 4
  27. 5
  28. 6
  29. 7
  30. 8
  31. 9
  32. 10
  33. 11
  34. 12
  35. 13
  36. 14
  37. 15
  38. 16
  39. 17
  40. 18
  41. 19
  42. 20
  43. 21
  44. 22
  45. 23
  46. 24
  47. 25

在添加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()等回调

在这里插入图片描述

二、Dialog的消失

1、dismiss

  1. private final Runnable mDismissAction = this::dismissDialog;
  2. public void dismiss() {
  3. if (Looper.myLooper() == mHandler.getLooper()) {
  4. dismissDialog();
  5. } else {
  6. mHandler.post(mDismissAction);
  7. }
  8. }
  9. 1
  10. 2
  11. 3
  12. 4
  13. 5
  14. 6
  15. 7
  16. 8
  17. 9

保证UI操作都在主线程执行,而且引用了Java8新特性写法this::dismissDialog,最后都会调用dismissDialog()

2、dismissDialog

  1. void dismissDialog() {
  2. if (mDecor == null || !mShowing) {
  3. return;
  4. }
  5. if (mWindow.isDestroyed()) {
  6. Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
  7. return;
  8. }
  9. try {
  10. //这里移除DecorView
  11. mWindowManager.removeViewImmediate(mDecor);
  12. } finally {
  13. if (mActionMode != null) {
  14. mActionMode.finish();
  15. }
  16. mDecor = null;
  17. mWindow.closeAllPanels();
  18. onStop();
  19. mShowing = false;
  20. sendDismissMessage();
  21. }
  22. }
  23. 1
  24. 2
  25. 3
  26. 4
  27. 5
  28. 6
  29. 7
  30. 8
  31. 9
  32. 10
  33. 11
  34. 12
  35. 13
  36. 14
  37. 15
  38. 16
  39. 17
  40. 18
  41. 19
  42. 20
  43. 21
  44. 22
  45. 23
  46. 24
  47. 25

从show中知道,我们将DecorView加入到WindowManager中去,所以这里移除的是DecorView

3、WindowManagerImpl.removeViewImmediate

  1. public void removeViewImmediate(View view) {
  2. //委托给mGlobal来进行实现
  3. mGlobal.removeView(view, true);
  4. }
  5. 1
  6. 2
  7. 3
  8. 4

同样的交给WindowManagerGlobal去处理

4、WindowManagerGlobal.removeView

  1. public void removeView(View view, boolean immediate) {
  2. if (view == null) {
  3. throw new IllegalArgumentException("view must not be null");
  4. }
  5. synchronized (mLock) {
  6. //待remove view的索引
  7. int index = findViewLocked(view, true);
  8. //mRoots保存着每一个viewRootImpl对象
  9. View curView = mRoots.get(index).getView();
  10. //真正对view进行了remove操作
  11. removeViewLocked(index, immediate);
  12. if (curView == view) {
  13. return;
  14. }
  15. throw new IllegalStateException("Calling with view " + view
  16. + " but the ViewAncestor is attached to " + curView);
  17. }
  18. }
  19. 1
  20. 2
  21. 3
  22. 4
  23. 5
  24. 6
  25. 7
  26. 8
  27. 9
  28. 10
  29. 11
  30. 12
  31. 13
  32. 14
  33. 15
  34. 16
  35. 17
  36. 18
  37. 19
  38. 20

找到对应要移除的View后进行View逻辑处理工作

5、WindowManagerGlobal.removeViewLocked

  1. private void removeViewLocked(int index, boolean immediate) {
  2. ViewRootImpl root = mRoots.get(index);
  3. View view = root.getView();
  4. if (view != null) {
  5. InputMethodManager imm = InputMethodManager.getInstance();
  6. if (imm != null) {
  7. imm.windowDismissed(mViews.get(index).getWindowToken());
  8. }
  9. }
  10. //重点在ViewRootImpl中的die方法中
  11. boolean deferred = root.die(immediate);
  12. if (view != null) {
  13. view.assignParent(null);
  14. if (deferred) {
  15. mDyingViews.add(view);
  16. }
  17. }
  18. }
  19. 1
  20. 2
  21. 3
  22. 4
  23. 5
  24. 6
  25. 7
  26. 8
  27. 9
  28. 10
  29. 11
  30. 12
  31. 13
  32. 14
  33. 15
  34. 16
  35. 17
  36. 18
  37. 19

找到对应的ViewRootImpl,进行移除并释放工作

6、ViewRootImpl.die

  1. boolean die(boolean immediate) {
  2. if (immediate && !mIsInTraversal) {
  3. //继续跟踪
  4. doDie();
  5. return false;
  6. }
  7. if (!mIsDrawing) {
  8. destroyHardwareRenderer();
  9. } else {
  10. Log.e(TAG, "Attempting to destroy the window while drawing!\n" +
  11. " window=" + this + ", title=" + mWindowAttributes.getTitle());
  12. }
  13. mHandler.sendEmptyMessage(MSG_DIE);
  14. return true;
  15. }
  16. 1
  17. 2
  18. 3
  19. 4
  20. 5
  21. 6
  22. 7
  23. 8
  24. 9
  25. 10
  26. 11
  27. 12
  28. 13
  29. 14
  30. 15
  31. 16

7、ViewRootImpl.doDie

  1. void doDie() {
  2. checkThread();
  3. if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
  4. synchronized (this) {
  5. if (mRemoved) {
  6. return;
  7. }
  8. mRemoved = true;
  9. if (mAdded) {
  10. //这里是真正移除Dialog的View
  11. dispatchDetachedFromWindow();
  12. }
  13. if (mAdded && !mFirst) {
  14. //硬件渲染destroy
  15. destroyHardwareRenderer();
  16. if (mView != null) {
  17. int viewVisibility = mView.getVisibility();
  18. boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
  19. if (mWindowAttributesChanged || viewVisibilityChanged) {
  20. // If layout params have been changed, first give them
  21. // to the window manager to make sure it has the correct
  22. // animation info.
  23. try {
  24. if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
  25. & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
  26. mWindowSession.finishDrawing(mWindow);
  27. }
  28. } catch (RemoteException e) {
  29. }
  30. }
  31. //Surface的释放
  32. mSurface.release();
  33. }
  34. }
  35. mAdded = false;
  36. }
  37. //移除之前存储的变量
  38. WindowManagerGlobal.getInstance().doRemoveView(this);
  39. }
  40. 1
  41. 2
  42. 3
  43. 4
  44. 5
  45. 6
  46. 7
  47. 8
  48. 9
  49. 10
  50. 11
  51. 12
  52. 13
  53. 14
  54. 15
  55. 16
  56. 17
  57. 18
  58. 19
  59. 20
  60. 21
  61. 22
  62. 23
  63. 24
  64. 25
  65. 26
  66. 27
  67. 28
  68. 29
  69. 30
  70. 31
  71. 32
  72. 33
  73. 34
  74. 35
  75. 36
  76. 37
  77. 38
  78. 39
  79. 40
  80. 41
  81. 42

保证线程安全后,做移除和释放工作

8、WindowManagerGlobal.doRemoveView

一般程序最后的工作都是释放工作,移除之前存储的变量

  1. void doRemoveView(ViewRootImpl root) {
  2. synchronized (mLock) {
  3. final int index = mRoots.indexOf(root);
  4. if (index >= 0) {
  5. //释放工作
  6. mRoots.remove(index);
  7. mParams.remove(index);
  8. final View view = mViews.remove(index);
  9. mDyingViews.remove(view);
  10. }
  11. }
  12. if (HardwareRenderer.sTrimForeground && HardwareRenderer.isAvailable()) {
  13. doTrimForeground();
  14. }
  15. }
  16. 1
  17. 2
  18. 3
  19. 4
  20. 5
  21. 6
  22. 7
  23. 8
  24. 9
  25. 10
  26. 11
  27. 12
  28. 13
  29. 14
  30. 15

9、ViewRootImpl.dispatchDetachedFromWindow

  1. void dispatchDetachedFromWindow() {
  2. if (mView != null && mView.mAttachInfo != null) {
  3. mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
  4. //此方法会回调onDetachedFromWindow方法,会做资源的回收
  5. mView.dispatchDetachedFromWindow();
  6. }
  7. mAccessibilityInteractionConnectionManager.ensureNoConnection();
  8. mAccessibilityManager.removeAccessibilityStateChangeListener(
  9. mAccessibilityInteractionConnectionManager);
  10. mAccessibilityManager.removeHighTextContrastStateChangeListener(
  11. mHighContrastTextManager);
  12. removeSendWindowContentChangedCallback()
  13. destroyHardwareRenderer();
  14. setAccessibilityFocus(null, null);
  15. mView.assignParent(null);
  16. mView = null;
  17. mAttachInfo.mRootView = null;
  18. mSurface.release();
  19. if (mInputQueueCallback != null && mInputQueue != null) {
  20. mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
  21. mInputQueue.dispose();
  22. mInputQueueCallback = null;
  23. mInputQueue = null;
  24. }
  25. if (mInputEventReceiver != null) {
  26. mInputEventReceiver.dispose();
  27. mInputEventReceiver = null;
  28. }
  29. try {
  30. //这里调用了mWindowSession的remove方法,在WindowManagerService层通过IPC机制完成真正的window删除
  31. mWindowSession.remove(mWindow);
  32. } catch (RemoteException e) {
  33. }
  34. // Dispose the input channel after removing the window so the Window Manager
  35. // doesn't interpret the input channel being closed as an abnormal termination.
  36. if (mInputChannel != null) {
  37. mInputChannel.dispose();
  38. mInputChannel = null;
  39. }
  40. mDisplayManager.unregisterDisplayListener(mDisplayListener);
  41. unscheduleTraversals();
  42. }
  43. 1
  44. 2
  45. 3
  46. 4
  47. 5
  48. 6
  49. 7
  50. 8
  51. 9
  52. 10
  53. 11
  54. 12
  55. 13
  56. 14
  57. 15
  58. 16
  59. 17
  60. 18
  61. 19
  62. 20
  63. 21
  64. 22
  65. 23
  66. 24
  67. 25
  68. 26
  69. 27
  70. 28
  71. 29
  72. 30
  73. 31
  74. 32
  75. 33
  76. 34
  77. 35
  78. 36
  79. 37
  80. 38
  81. 39
  82. 40
  83. 41
  84. 42
  85. 43
  86. 44
  87. 45
  88. 46
  89. 47
  90. 48
  91. 49
  92. 50
  93. 51

到最后会和添加View的时候完成闭环,还是通过WindowSession的IPC机制去调用的,最后在WindowManagerService层通过IPC机制去实现的

总结

  1. Dialog的dismiss和show形成闭环,调用的过程是相似的,只不过多了资源的释放环节

在这里插入图片描述

DialogFragment

DialogFragment本身继承自Fragment

  1. public class DialogFragment extends Fragment
  2. implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener
  3. 1
  4. 2

在平时中,我们需要自定义WeDialogFragment,而且在正式开发中踩过的坑:

  • 需要对参数进行onSaveInstanceState操作,这类操作主要是防止异步吊起DialogFragment报nullPoint的Bug
  • 需要重写show(),对show做一层弹出时候的保护,这类操作主要是防止异步吊起DialogFragment报onSaveInstanceState的Bug

    public class WeDialogFragment extends DialogFragment {

    1. @Override
    2. public void onSaveInstanceState(Bundle outState) {
    3. super.onSaveInstanceState(outState);
    4. Bundle bundle = new Bundle();
    5. bundle.putString(BUNDLE_TITLE, title);
    6. }
    7. @Override
    8. public void onCreate(@Nullable Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState);
    10. setStyle(DialogFragment.STYLE_NO_TITLE, R.style.WeDialog);
    11. if (savedInstanceState != null) {
    12. title = savedInstanceState.getString(BUNDLE_TITLE);
    13. }
    14. }
    15. @Override
    16. public Dialog onCreateDialog(Bundle savedInstanceState) {
    17. Dialog dialog = super.onCreateDialog(savedInstanceState);
    18. dialog.setCanceledOnTouchOutside(true);
    19. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    20. dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
    21. dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    22. return dialog;
    23. }
    24. @Nullable
    25. @Override
    26. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    27. @Nullable Bundle savedInstanceState) {
    28. return inflater.inflate(R.layout.view_fragment_dialog, container, false);
    29. }
    30. @Override
    31. public void show(FragmentManager manager, String tag) {
    32. if (!manager.isStateSaved()) {
    33. super.show(manager, tag);
    34. }
    35. }

    }

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43

然后在Activity中弹出DialogFragment

  1. WeDialogFragment weDialogFragment = new WeDialogFragment();
  2. weDialogFragment.show(activity.getSupportFragmentManager(),"weDialogFragment");
  3. 1
  4. 2

一、DialogFragment的显示

1、DialogFragment.show

  1. public void show(FragmentManager manager, String tag) {
  2. mDismissed = false;
  3. mShownByMe = true;
  4. FragmentTransaction ft = manager.beginTransaction();
  5. ft.add(this, tag);
  6. ft.commit();
  7. }
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6
  14. 7

show的方法其实就是对Fragment的处理,将Fragment添加到Fragment栈中

二、DialogFragment的隐藏

1、DialogFragment.dismiss

  1. public void dismiss() {
  2. dismissInternal(false);
  3. }
  4. public void dismissAllowingStateLoss() {
  5. dismissInternal(true);
  6. }
  7. void dismissInternal(boolean allowStateLoss) {
  8. if (mDismissed) {
  9. return;
  10. }
  11. mDismissed = true;
  12. mShownByMe = false;
  13. if (mDialog != null) {
  14. mDialog.dismiss();
  15. }
  16. mViewDestroyed = true;
  17. if (mBackStackId >= 0) {
  18. getFragmentManager().popBackStack(mBackStackId,
  19. FragmentManager.POP_BACK_STACK_INCLUSIVE);
  20. mBackStackId = -1;
  21. } else {
  22. FragmentTransaction ft = getFragmentManager().beginTransaction();
  23. ft.remove(this);
  24. if (allowStateLoss) {
  25. ft.commitAllowingStateLoss();
  26. } else {
  27. ft.commit();
  28. }
  29. }
  30. }
  31. 1
  32. 2
  33. 3
  34. 4
  35. 5
  36. 6
  37. 7
  38. 8
  39. 9
  40. 10
  41. 11
  42. 12
  43. 13
  44. 14
  45. 15
  46. 16
  47. 17
  48. 18
  49. 19
  50. 20
  51. 21
  52. 22
  53. 23
  54. 24
  55. 25
  56. 26
  57. 27
  58. 28
  59. 29
  60. 30
  61. 31
  62. 32

dismiss的方法也是对Fragment的处理,将Fragment移除到Fragment栈中

三、Dialog的创建

1、DialogFragment.onCreateDialog

  1. @NonNull
  2. public Dialog onCreateDialog(Bundle savedInstanceState) {
  3. return new Dialog(getActivity(), getTheme());
  4. }
  5. 1
  6. 2
  7. 3
  8. 4

和创建普通的Dialog没什么区别,我们重写该方法,可以自定义弹出AlertDialog等其他自定义Dialog

四、Dialog的视图

1、DialogFragment.onActivityCreated

  1. @Override
  2. public void onActivityCreated(Bundle savedInstanceState) {
  3. super.onActivityCreated(savedInstanceState);
  4. if (!mShowsDialog) {
  5. return;
  6. }
  7. //拿到的就是onCreateView返回值的view对象,具体可以在Fragment源码找到
  8. View view = getView();
  9. if (view != null) {
  10. if (view.getParent() != null) {
  11. throw new IllegalStateException(
  12. "DialogFragment can not be attached to a container view");
  13. }
  14. //真正设置view
  15. mDialog.setContentView(view);
  16. }
  17. final Activity activity = getActivity();
  18. if (activity != null) {
  19. mDialog.setOwnerActivity(activity);
  20. }
  21. mDialog.setCancelable(mCancelable);
  22. mDialog.setOnCancelListener(this);
  23. mDialog.setOnDismissListener(this);
  24. if (savedInstanceState != null) {
  25. Bundle dialogState = savedInstanceState.getBundle(SAVED_DIALOG_STATE_TAG);
  26. if (dialogState != null) {
  27. mDialog.onRestoreInstanceState(dialogState);
  28. }
  29. }
  30. }
  31. 1
  32. 2
  33. 3
  34. 4
  35. 5
  36. 6
  37. 7
  38. 8
  39. 9
  40. 10
  41. 11
  42. 12
  43. 13
  44. 14
  45. 15
  46. 16
  47. 17
  48. 18
  49. 19
  50. 20
  51. 21
  52. 22
  53. 23
  54. 24
  55. 25
  56. 26
  57. 27
  58. 28
  59. 29
  60. 30
  61. 31
  62. 32

在Activity创建的时候,Fragment的周期会回调onActivityCreated,从而对Dialog设置视图

五、Dialog的显示隐藏

Dialog显示隐藏就简单了,随着Fragment的生命周期显示和隐藏,直接看代码就行了

  1. @Override
  2. public void onStart() {
  3. super.onStart();
  4. if (mDialog != null) {
  5. mViewDestroyed = false;
  6. mDialog.show();
  7. }
  8. }
  9. @Override
  10. public void onStop() {
  11. super.onStop();
  12. if (mDialog != null) {
  13. mDialog.hide();
  14. }
  15. }
  16. 1
  17. 2
  18. 3
  19. 4
  20. 5
  21. 6
  22. 7
  23. 8
  24. 9
  25. 10
  26. 11
  27. 12
  28. 13
  29. 14
  30. 15
  31. 16

总结

DialogFragment = Fragment + Dialog,DialogFragment本身继承Fragment,Fragment只是用来依附在Activity上,可以监听Activity的生命周期,从而去通知Dialog做对应的操作,而Dialog才是我们正在显示在屏幕上的弹窗,而非一个Fragment。这里的Dialog真正显示出来的View是从onCreateView()中获取view后,在源码中调用dialog的setContentView()显示出来的

发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读