【问题标题】:Android App exits Fullscreen when showing a Dialog on a Fullscreen ActivityAndroid 应用程序在全屏 Activity 上显示对话框时退出全屏
【发布时间】:2021-07-26 20:19:49
【问题描述】:

根据请求,我正在尝试使 Android 应用程序全屏显示。我关注了Enable fullscreen mode,但是在显示对话框时,导航菜单(主页按钮、后退按钮等)会在显示对话框时再次显示。有没有办法禁用它?

我基于 Fullscreen Activity 模板制作了一个示例应用,我观察到了相同的行为:

【问题讨论】:

    标签: android dialog fullscreen android-fullscreen


    【解决方案1】:

    对话框窗口默认是可聚焦的,可聚焦的窗口会导致退出全屏模式。

    对于解决方法,您可以尝试将FLAG_NOT_FOCUSABLE 标志设置为您的对话框,如here 所述,但请注意系统对话框(如 ANR)仍会导致退出。

    【讨论】:

    • 感谢分享!我为基础对话框类稍微调整了解决方案。发表评论太长了,所以我会将其作为另一个答案发布,但我会将您的答案标记为已接受。 :)
    【解决方案2】:

    分享我的解决方案,基于link@ceribadev分享的答案:

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
    
        // Here's the magic..
        try {
            // Set the dialog to not focusable (makes navigation ignore us adding the window)
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    
            // Show the dialog!
            dialog.setOnShowListener(dialogInterface -> {
                // Set the dialog to immersive
                dialog.getWindow().getDecorView().setSystemUiVisibility(dialog.getOwnerActivity().getWindow().getDecorView().getSystemUiVisibility());
    
                // Clear the not focusable flag from the window
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return dialog;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多