美文网首页
从Dialog闪退看WMS部分源码2020-12-09

从Dialog闪退看WMS部分源码2020-12-09

作者: 木易白水 | 来源:发表于2020-12-09 09:29 被阅读0次

    一 问题引出

    前两天做一个需求,需求中有界面有加载网络,加载网络的时候要弹个dialog显示正在加载,等到服务器返回数据后,dialog消失。
    开发前,搜索了一下,发现google推荐DialogFragment,但是有很多评论说坑比较多,所以我就没有用它而是直接用了Dialog,然后就出问题了。(其实想想也知道,google推出DialogFragment取代Dialog肯定是Dialog不够好的,就像用DataStore取代SharedPreferences一样。)
    出现的具体问题就是,activity 生命周期内去show一个dialog,然后activity 在destroy之后再去dismiss这个dialog,虽然在dismiss之前有判断dialog.isShowing(),但还是会闪退。因为activity destroy之后,WindowManagerGlobal中的mView中已经清空了。再次去移除时,找不到,直接throw new IllegalArgumentException("View=" + view + " not attached to window manager");

    val dialog:Dialog = Dialog(activity, themeId)
    if (!activity.isDestroyed) {
        dialog.show()
    }
    ...
    if (dialog != null && dialog.isShowing) {
        dialog.dismiss()  //闪退
    }
    

    二 报错日志:

    11-30 17:26:08.599 E/AndroidRuntime(12701): FATAL EXCEPTION: main
    11-30 17:26:08.599 E/AndroidRuntime(12701): Process: com.mobile.myapp, PID: 12701
    11-30 17:26:08.599 E/AndroidRuntime(12701): java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{2fb63f2 V.E...... R......D 0,0-720,1440} not attached to window manager
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:448)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:372)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.app.Dialog.dismissDialog(Dialog.java:372)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.app.Dialog.dismiss(Dialog.java:351)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at com.luck.game.adv.ReaperAdvRequestUtilsKt$createInteractionExpressAdListener$1.run(SourceFile:141)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.os.Handler.handleCallback(Handler.java:815)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.os.Handler.dispatchMessage(Handler.java:104)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.os.Looper.loop(Looper.java:207)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at android.app.ActivityThread.main(ActivityThread.java:5821)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at java.lang.reflect.Method.invoke(Native Method)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
    11-30 17:26:08.599 E/AndroidRuntime(12701):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
    

    三 解决方法

    解决方法也很简答,dismiss之前多加一个判断,activity是否已经销毁了。

    if (dialog != null && dialog.isShowing && !activity.isDestroyed) {
        dialog.dismiss()
    }
    

    google推出DialogFragment,解决了生命周期的问题,而这个报错就是由生命周期引起的。所以后续的需求,还是尽量用google推荐的把,毕竟咱整个android都是它们造的,它自己肯定很了解这些缺陷,推荐的这些东西肯定是解决了一些问题的。

    四 原因分析:

    1. 先看Dialog的源码,发现在dismiss之前有判断标志位,所以直观的认为即使activity销毁了,也是不会报错的:
    /**
     * Dismiss this dialog, removing it from the screen. This method can be
     * invoked safely from any thread.  Note that you should not override this
     * method to do cleanup when the dialog is dismissed, instead implement
     * that in {@link #onStop}.
     */
    @Override
    public void dismiss() {
        if (Looper.myLooper() == mHandler.getLooper()) {
            dismissDialog();
        } else {
            mHandler.post(mDismissAction);
        }
    }
    
    void dismissDialog() {
        if (mDecor == null || !mShowing) {  //有判断标志位mShowing,所以认为不太容易出现异常,即使activity销毁了。
            return;
        }
    
        if (mWindow.isDestroyed()) {
            Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
            return;
        }
    
        try {
            mWindowManager.removeViewImmediate(mDecor);  // 但是在这里,它实实在在的抛了异常。
        } finally {
            if (mActionMode != null) {
                mActionMode.finish();
            }
            mDecor = null;
            mWindow.closeAllPanels();
            onStop();
            mShowing = false;
    
            sendDismissMessage();
        }
    }
    
    1. 其实在activity销毁之后,这个标志位并没有改变,所以通过判断这个标志位,是不足以避免抛出异常的。

    2. 从报错日志堆栈中可以看出,抛出异常的真正原因在
      E/AndroidRuntime(12701): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:448)
      看这里的源代码:

    //WindowManagerGlobal.java
    private int findViewLocked(View view, boolean required) {
        final int index = mViews.indexOf(view);
        if (required && index < 0) {
            throw new IllegalArgumentException("View=" + view + " not attached to window manager");
        }
        return index;
    }
    

    可见,是mViews里面已经没有Dialog的view了,所以抛出了异常。

    1. mViews里面什么时候移除的Dialog的view呢?在源码中搜索,发现有如下代码:
    //WindowManagerGlobal.java
    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);  //移除对应的view
                mDyingViews.remove(view);
            }
        }
        if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
            doTrimForeground();
        }
    }
    

    而在ViewRootImpl.java中,又有如下代码:

    void doDie() {
        checkThread();
        if (LOCAL_LOGV) Log.v(mTag, "DIE in " + this + " of " + mSurface);
        synchronized (this) {
            if (mRemoved) {
                return;
            }
            mRemoved = true;
            if (mAdded) {
                dispatchDetachedFromWindow();
            }
    
            if (mAdded && !mFirst) {
                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) {
                        }
                    }
    
                    mSurface.release();
                }
            }
    
            mAdded = false;
        }
        WindowManagerGlobal.getInstance().doRemoveView(this); //调用移除
    }
    
    1. 确认ViewRootImpl.java的doDie()方法有在activity销毁时调用: 在activity销毁的时候,在一个自定义view里面的onDetachedFromWindow中,主动抛出一个空指针异常,并捕获和打印其堆栈:
    12-24 10:22:48.160 W/System.err(30566): java.lang.NullPointerException: WeatherViewPager onDetachedFromWindow YQ
    12-24 10:22:48.160 W/System.err(30566):     at net.test.myapp.WeatherViewPager.onDetachedFromWindow(WeatherViewPager.java:207)
    12-24 10:22:48.160 W/System.err(30566):     at android.view.View.dispatchDetachedFromWindow(View.java:19690)
    12-24 10:22:48.160 W/System.err(30566):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3819)
    12-24 10:22:48.160 W/System.err(30566):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3811)
    12-24 10:22:48.160 W/System.err(30566):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3811)
    12-24 10:22:48.160 W/System.err(30566):     at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:4204)
    12-24 10:22:48.161 W/System.err(30566):     at android.view.ViewRootImpl.doDie(ViewRootImpl.java:7223)  //确认有调用
    12-24 10:22:48.161 W/System.err(30566):     at android.view.ViewRootImpl.die(ViewRootImpl.java:7200)
    12-24 10:22:48.161 W/System.err(30566):     at android.view.WindowManagerGlobal.removeViewLocked(WindowManagerGlobal.java:527)
    12-24 10:22:48.161 W/System.err(30566):     at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:465)
    12-24 10:22:48.161 W/System.err(30566):     at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:126)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5122)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2065)
    12-24 10:22:48.161 W/System.err(30566):     at android.os.Handler.dispatchMessage(Handler.java:107)
    12-24 10:22:48.161 W/System.err(30566):     at android.os.Looper.loop(Looper.java:214)
    12-24 10:22:48.161 W/System.err(30566):     at android.app.ActivityThread.main(ActivityThread.java:7656)
    12-24 10:22:48.161 W/System.err(30566):     at java.lang.reflect.Method.invoke(Native Method)
    12-24 10:22:48.161 W/System.err(30566):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
    12-24 10:22:48.161 W/System.err(30566):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:948)
    
    1. 完工,今日事今日毕。一直想着准备好了再记录,而总有不满意和不完美的地方,拖着事情不闭环,不如先记录下来,后续再慢慢完善和润色语言

    相关文章

      网友评论

          本文标题:从Dialog闪退看WMS部分源码2020-12-09

          本文链接:https://www.haomeiwen.com/subject/rewcgktx.html