Android 在全屏显示对话框时应用退出全屏 Activity
Android App exits Fullscreen when showing a Dialog on a Fullscreen Activity
根据请求,我正在尝试让 Android 应用程序全屏显示。我已关注 Enable fullscreen mode,但在显示对话框时,导航菜单(主页按钮、后退按钮等)会在显示对话框时再次显示。有没有办法禁用它?
我基于全屏 Activity 模板制作了一个示例应用程序,我观察到相同的行为:
对话框 window 默认是可聚焦的,可聚焦 windows 导致退出全屏模式。
对于解决方法,您可以尝试将 FLAG_NOT_FOCUSABLE
标志设置为您的对话框,如所述 here 但请注意,ANR 等系统对话框仍会导致退出。
分享我的解决方案,基于 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;
}
根据请求,我正在尝试让 Android 应用程序全屏显示。我已关注 Enable fullscreen mode,但在显示对话框时,导航菜单(主页按钮、后退按钮等)会在显示对话框时再次显示。有没有办法禁用它?
我基于全屏 Activity 模板制作了一个示例应用程序,我观察到相同的行为:
对话框 window 默认是可聚焦的,可聚焦 windows 导致退出全屏模式。
对于解决方法,您可以尝试将 FLAG_NOT_FOCUSABLE
标志设置为您的对话框,如所述 here 但请注意,ANR 等系统对话框仍会导致退出。
分享我的解决方案,基于 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;
}