android应用程序在全屏活动上显示对话框时退出全屏

5fjcxozz  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(328)

根据请求,我正在尝试使android应用程序全屏显示。我遵循了启用全屏模式,但当显示对话框时,导航菜单(主页按钮、后退按钮等)会在显示对话框时再次显示。有没有办法禁用此功能?
我基于全屏活动模板制作了一个示例应用程序,我观察到了相同的行为:

irtuqstp

irtuqstp1#

对话框窗口在默认情况下是可聚焦的,可聚焦的窗口导致从全屏模式退出。
对于解决方法,您可以尝试设置 FLAG_NOT_FOCUSABLE 按此处所述标记对话框,但请注意,系统对话框(如anr)仍将导致退出。

iezvtpos

iezvtpos2#

根据中的答案共享我的解决方案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;
}

相关问题