Android在DialogFragment推出后,就已经不推荐继续使用Dialog,可替换为DialogFragment,其实DialogFragment只不过是对增加一层看不到的Fragment,用于监听生命周期,在Activity退出的时候会自动回收Dialog弹窗
@android:id/content
@android:id/content
在平时中,简单的弹出Dialog只需要这句话
new Dialog(MainActivity.this).show();
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、dismiss
private final Runnable mDismissAction = this::dismissDialog;
public void dismiss() {
if (Looper.myLooper() == mHandler.getLooper()) {
dismissDialog();
} else {
mHandler.post(mDismissAction);
}
}
保证UI操作都在主线程执行,而且引用了Java8新特性写法this::dismissDialog
,最后都会调用dismissDialog()
2、dismissDialog
void dismissDialog() {
if (mDecor == null || !mShowing) {
return;
}
if (mWindow.isDestroyed()) {
Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
return;
}
try {
//这里移除DecorView
mWindowManager.removeViewImmediate(mDecor);
} finally {
if (mActionMode != null) {
mActionMode.finish();
}
mDecor = null;
mWindow.closeAllPanels();
onStop();
mShowing = false;
sendDismissMessage();
}
}
从show中知道,我们将DecorView加入到WindowManager中去,所以这里移除的是DecorView
3、WindowManagerImpl.removeViewImmediate
public void removeViewImmediate(View view) {
//委托给mGlobal来进行实现
mGlobal.removeView(view, true);
}
同样的交给WindowManagerGlobal去处理
4、WindowManagerGlobal.removeView
public void removeView(View view, boolean immediate) {
if (view == null) {
throw new IllegalArgumentException("view must not be null");
}
synchronized (mLock) {
//待remove view的索引
int index = findViewLocked(view, true);
//mRoots保存着每一个viewRootImpl对象
View curView = mRoots.get(index).getView();
//真正对view进行了remove操作
removeViewLocked(index, immediate);
if (curView == view) {
return;
}
throw new IllegalStateException("Calling with view " + view
+ " but the ViewAncestor is attached to " + curView);
}
}
找到对应要移除的View后进行View逻辑处理工作
5、WindowManagerGlobal.removeViewLocked
private void removeViewLocked(int index, boolean immediate) {
ViewRootImpl root = mRoots.get(index);
View view = root.getView();
if (view != null) {
InputMethodManager imm = InputMethodManager.getInstance();
if (imm != null) {
imm.windowDismissed(mViews.get(index).getWindowToken());
}
}
//重点在ViewRootImpl中的die方法中
boolean deferred = root.die(immediate);
if (view != null) {
view.assignParent(null);
if (deferred) {
mDyingViews.add(view);
}
}
}
找到对应的ViewRootImpl,进行移除并释放工作
6、ViewRootImpl.die
boolean die(boolean immediate) {
if (immediate && !mIsInTraversal) {
//继续跟踪
doDie();
return false;
}
if (!mIsDrawing) {
destroyHardwareRenderer();
} else {
Log.e(TAG, "Attempting to destroy the window while drawing!\n" +
" window=" + this + ", title=" + mWindowAttributes.getTitle());
}
mHandler.sendEmptyMessage(MSG_DIE);
return true;
}
7、ViewRootImpl.doDie
void doDie() {
checkThread();
if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
synchronized (this) {
if (mRemoved) {
return;
}
mRemoved = true;
if (mAdded) {
//这里是真正移除Dialog的View
dispatchDetachedFromWindow();
}
if (mAdded && !mFirst) {
//硬件渲染destroy
destroyHardwareRenderer();
if (mView != null) {
int viewVisibility = mView.getVisibility();
boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
if (mWindowAttributesChanged || viewVisibilityChanged) {
// If layout params have been changed, first give them
// to the window manager to make sure it has the correct
// animation info.
try {
if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
& WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
mWindowSession.finishDrawing(mWindow);
}
} catch (RemoteException e) {
}
}
//Surface的释放
mSurface.release();
}
}
mAdded = false;
}
//移除之前存储的变量
WindowManagerGlobal.getInstance().doRemoveView(this);
}
保证线程安全后,做移除和释放工作
8、WindowManagerGlobal.doRemoveView
一般程序最后的工作都是释放工作,移除之前存储的变量
void doRemoveView(ViewRootImpl root) {
synchronized (mLock) {
final int index = mRoots.indexOf(root);
if (index >= 0) {
//释放工作
mRoots.remove(index);
mParams.remove(index);
final View view = mViews.remove(index);
mDyingViews.remove(view);
}
}
if (HardwareRenderer.sTrimForeground && HardwareRenderer.isAvailable()) {
doTrimForeground();
}
}
9、ViewRootImpl.dispatchDetachedFromWindow
void dispatchDetachedFromWindow() {
if (mView != null && mView.mAttachInfo != null) {
mAttachInfo.mTreeObserver.dispatchOnWindowAttachedChange(false);
//此方法会回调onDetachedFromWindow方法,会做资源的回收
mView.dispatchDetachedFromWindow();
}
mAccessibilityInteractionConnectionManager.ensureNoConnection();
mAccessibilityManager.removeAccessibilityStateChangeListener(
mAccessibilityInteractionConnectionManager);
mAccessibilityManager.removeHighTextContrastStateChangeListener(
mHighContrastTextManager);
removeSendWindowContentChangedCallback();
destroyHardwareRenderer();
setAccessibilityFocus(null, null);
mView.assignParent(null);
mView = null;
mAttachInfo.mRootView = null;
mSurface.release();
if (mInputQueueCallback != null && mInputQueue != null) {
mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
mInputQueue.dispose();
mInputQueueCallback = null;
mInputQueue = null;
}
if (mInputEventReceiver != null) {
mInputEventReceiver.dispose();
mInputEventReceiver = null;
}
try {
//这里调用了mWindowSession的remove方法,在WindowManagerService层通过IPC机制完成真正的window删除
mWindowSession.remove(mWindow);
} catch (RemoteException e) {
}
// Dispose the input channel after removing the window so the Window Manager
// doesn't interpret the input channel being closed as an abnormal termination.
if (mInputChannel != null) {
mInputChannel.dispose();
mInputChannel = null;
}
mDisplayManager.unregisterDisplayListener(mDisplayListener);
unscheduleTraversals();
}
到最后会和添加View的时候完成闭环,还是通过WindowSession
的IPC机制去调用的,最后在WindowManagerService层通过IPC机制去实现的
DialogFragment本身继承自Fragment
public class DialogFragment extends Fragment
implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener
在平时中,我们需要自定义WeDialogFragment,而且在正式开发中踩过的坑:
onSaveInstanceState
操作,这类操作主要是防止异步吊起DialogFragment报nullPoint的Bugshow()
,对show做一层弹出时候的保护,这类操作主要是防止异步吊起DialogFragment报onSaveInstanceState的Bugpublic class WeDialogFragment extends DialogFragment {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle bundle = new Bundle();
bundle.putString(BUNDLE_TITLE, title);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.WeDialog);
if (savedInstanceState != null) {
title = savedInstanceState.getString(BUNDLE_TITLE);
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_fragment_dialog, container, false);
}
@Override
public void show(FragmentManager manager, String tag) {
if (!manager.isStateSaved()) {
super.show(manager, tag);
}
}
}
然后在Activity中弹出DialogFragment
WeDialogFragment weDialogFragment = new WeDialogFragment();
weDialogFragment.show(activity.getSupportFragmentManager(),"weDialogFragment");
1、DialogFragment.show
public void show(FragmentManager manager, String tag) {
mDismissed = false;
mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
}
show的方法其实就是对Fragment的处理,将Fragment添加到Fragment栈中
1、DialogFragment.dismiss
public void dismiss() {
dismissInternal(false);
}
public void dismissAllowingStateLoss() {
dismissInternal(true);
}
void dismissInternal(boolean allowStateLoss) {
if (mDismissed) {
return;
}
mDismissed = true;
mShownByMe = false;
if (mDialog != null) {
mDialog.dismiss();
}
mViewDestroyed = true;
if (mBackStackId >= 0) {
getFragmentManager().popBackStack(mBackStackId,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
mBackStackId = -1;
} else {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.remove(this);
if (allowStateLoss) {
ft.commitAllowingStateLoss();
} else {
ft.commit();
}
}
}
dismiss的方法也是对Fragment的处理,将Fragment移除到Fragment栈中
1、DialogFragment.onCreateDialog
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme());
}
和创建普通的Dialog没什么区别,我们重写该方法,可以自定义弹出AlertDialog等其他自定义Dialog
1、DialogFragment.onActivityCreated
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (!mShowsDialog) {
return;
}
//拿到的就是onCreateView返回值的view对象,具体可以在Fragment源码找到
View view = getView();
if (view != null) {
if (view.getParent() != null) {
throw new IllegalStateException(
"DialogFragment can not be attached to a container view");
}
//真正设置view
mDialog.setContentView(view);
}
final Activity activity = getActivity();
if (activity != null) {
mDialog.setOwnerActivity(activity);
}
mDialog.setCancelable(mCancelable);
mDialog.setOnCancelListener(this);
mDialog.setOnDismissListener(this);
if (savedInstanceState != null) {
Bundle dialogState = savedInstanceState.getBundle(SAVED_DIALOG_STATE_TAG);
if (dialogState != null) {
mDialog.onRestoreInstanceState(dialogState);
}
}
}
在Activity创建的时候,Fragment的周期会回调onActivityCreated
,从而对Dialog设置视图
Dialog显示隐藏就简单了,随着Fragment的生命周期显示和隐藏,直接看代码就行了
@Override
public void onStart() {
super.onStart();
if (mDialog != null) {
mViewDestroyed = false;
mDialog.show();
}
}
@Override
public void onStop() {
super.onStop();
if (mDialog != null) {
mDialog.hide();
}
}
DialogFragment = Fragment + Dialog,DialogFragment本身继承Fragment,Fragment只是用来依附在Activity上,可以监听Activity的生命周期,从而去通知Dialog做对应的操作,而Dialog才是我们正在显示在屏幕上的弹窗,而非一个Fragment。这里的Dialog真正显示出来的View是从onCreateView()
中获取view后,在源码中调用dialog的setContentView()
显示出来的