How do I maintain the Immersive Mode in Dialogs?

后端 未结 7 699
醉酒成梦
醉酒成梦 2020-11-28 01:14

How do I maintain the new Immersive Mode when my activities display a custom Dialog?

I am using the code below to maintain the Immersive Mode in Dialogs, but with th

7条回答
  •  天涯浪人
    2020-11-28 01:31

    If you want to use onCreateDialog(), try this class. It works pretty well for me...

    public class ImmersiveDialogFragment extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                    .setTitle("Example Dialog")
                    .setMessage("Some text.")
                    .create();
    
            // Temporarily set the dialogs window to not focusable to prevent the short
            // popup of the navigation bar.
            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    
            return alertDialog;
    
        }
    
        public void showImmersive(Activity activity) {
    
            // Show the dialog.
            show(activity.getFragmentManager(), null);
    
            // It is necessary to call executePendingTransactions() on the FragmentManager
            // before hiding the navigation bar, because otherwise getWindow() would raise a
            // NullPointerException since the window was not yet created.
            getFragmentManager().executePendingTransactions();
    
            // Hide the navigation bar. It is important to do this after show() was called.
            // If we would do this in onCreateDialog(), we would get a requestFeature()
            // error.
            getDialog().getWindow().getDecorView().setSystemUiVisibility(
                getActivity().getWindow().getDecorView().getSystemUiVisibility()
            );
    
            // Make the dialogs window focusable again.
            getDialog().getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            );
    
        }
    
    }
    

    To show the dialog, do the following in your activity...

    new ImmersiveDialogFragment().showImmersive(this);
    

提交回复
热议问题