How do I maintain the Immersive Mode in Dialogs?

后端 未结 7 695
醉酒成梦
醉酒成梦 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:36

    Combining the answers here I made an abstract class that works in all cases:

    public abstract class ImmersiveDialogFragment extends DialogFragment {
    
        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);
    
            // Make the dialog non-focusable before showing it
            dialog.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
    
        @Override
        public void show(FragmentManager manager, String tag) {
            super.show(manager, tag);
            showImmersive(manager);
        }
    
        @Override
        public int show(FragmentTransaction transaction, String tag) {
            int result = super.show(transaction, tag);
            showImmersive(getFragmentManager());
            return result;
        }
    
        private void showImmersive(FragmentManager manager) {
            // 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.
            manager.executePendingTransactions();
    
            // Copy flags from the activity, assuming it's fullscreen.
            // 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
            );
        }
    }
    

提交回复
热议问题