[原创]andorid不持有dialog对象实现关闭activity的dialog、检测dialog是否弹出

拿不到dialog引用?不想设置为成员变量?却想在某个地方关闭dialog,或者检测当前activity是否被遮盖?

  private static ArrayList getWindowViews() {
        try {
            View rootView = null;
            Class wmgClass = Class.forName("android.view.WindowManagerGlobal");
            Object wmgInstnace = wmgClass.getMethod("getInstance").invoke(null, (Object[]) null);
            Field mViewsField = wmgClass.getDeclaredField("mViews");
            mViewsField.setAccessible(true);
            ArrayList o = (ArrayList) mViewsField.get(wmgInstnace);
            return o;

//            private final ArrayList mViews = new ArrayList();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
       List windowViews = getWindowViews();
                if (windowViews != null && windowViews.size() > 0 && windowViews.get(windowViews.size() - 1) != getWindow().getDecorView()) {
//                    BaseEditCodeActivity.super.onBackPressed();
                    View view = windowViews.get(windowViews.size() - 1); //parent = android.view.ViewRootImpl
//                    ViewRootImpl
                    getWindowManager().removeView(view);
                }

你可能感兴趣的:([原创]andorid不持有dialog对象实现关闭activity的dialog、检测dialog是否弹出)