如何在对话框中保持沉浸模式?

当我的活动显示自定义对话框时,如何维护新的沉浸模式?

我使用这段代码来保持对话框中的沉浸模式,但是通过这个解决scheme,当我启动我的自定义对话框时,NavBar出现不到一秒钟,然后它就消失了。

这是一个video,更好地解释了这个问题(看看NavBar出现在屏幕的底部):http: //youtu.be/epnd5ghey8g

我如何避免这种行为?

我申请中所有活动的父亲:

public abstract class ImmersiveActivity extends Activity { @SuppressLint("NewApi") private void disableImmersiveMode() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_FULLSCREEN); } } @SuppressLint("NewApi") private void enableImmersiveMode() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); } } /** * Set the Immersive mode or not according to its state: enabled or not. */ protected void updateSystemUiVisibility() { // Retrieve if the Immersive mode is enabled or not. boolean enabled = getSharedPreferences(Util.PREF_NAME, 0).getBoolean( "immersive_mode_enabled", true); if (enabled) enableImmersiveMode(); else disableImmersiveMode(); } @Override public void onResume() { super.onResume(); updateSystemUiVisibility(); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); updateSystemUiVisibility(); } } 


我的所有自定义对话框都在onCreate(. . .)方法中调用此方法:

 /** * Copy the visibility of the Activity that has started the dialog {@link mActivity}. If the * activity is in Immersive mode the dialog will be in Immersive mode too and vice versa. */ @SuppressLint("NewApi") private void copySystemUiVisibility() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { getWindow().getDecorView().setSystemUiVisibility( mActivity.getWindow().getDecorView().getSystemUiVisibility()); } } 


编辑 – 解决scheme(感谢Beaver6813,更多细节请看他的答案):

我所有的自定义对话框都以这种方式覆盖了show方法:

 /** * An hack used to show the dialogs in Immersive Mode (that is with the NavBar hidden). To * obtain this, the method makes the dialog not focusable before showing it, change the UI * visibility of the window like the owner activity of the dialog and then (after showing it) * makes the dialog focusable again. */ @Override public void show() { // Set the dialog to not focusable. getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); copySystemUiVisibility(); // Show the dialog with NavBar hidden. super.show(); // Set the dialog to focusable again. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); } 

在对这个问题进行了大量的研究之后,还有一个解决这个问题的方法,就是把这个Dialog类拆开。 即使您在将UI添加到pipe理器之前设置了UI可见性,也会在将对话框窗口添加到窗口pipe理器时显示导航栏。 在Android Immersive示例中,它评论说:

 // * Uses semi-transparent bars for the nav and status bars // * This UI flag will *not* be cleared when the user interacts with the UI. // When the user swipes, the bars will temporarily appear for a few seconds and then // disappear again. 

我相信这就是我们在这里看到的(当一个新的,可调焦的窗口视图被添加到pipe理器中时,用户交互被触发)。

我们如何解决这个问题? 使对话框在我们创build时不可对焦(所以我们不触发用户交互),然后在显示对话框后使其对焦。

 //Here's the magic.. //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.show(); //Set the dialog to immersive dialog.getWindow().getDecorView().setSystemUiVisibility( context.getWindow().getDecorView().getSystemUiVisibility()); //Clear the not focusable flag from the window dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); 

显然这不是理想的,但它似乎是一个Android的错误,他们应该检查,如果窗口具有沉浸式设置。

我已经更新了我的工作testing代码(原谅哈克混乱) Github 。 我已经在Nexus 5仿真器上testing过了,它可能会炸毁比KitKat更小的东西,但是它只能用于概念validation。

如果你想使用onCreateDialog() ,试试这个类。 它对我来说工作得非常好

 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 ); } } 

要显示对话框,请在您的活动中执行以下操作…

 new ImmersiveDialogFragment().showImmersive(this); 

对于您的信息,感谢@ Beaver6813的答案,我已经能够使用DialogFragment这个工作。

在我的DialogFragment的onCreateView方法中,我刚刚添加了以下内容:

  getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); getDialog().getWindow().getDecorView().setSystemUiVisibility(getActivity().getWindow().getDecorView().getSystemUiVisibility()); getDialog().setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { //Clear the not focusable flag from the window getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); //Update the WindowManager with the new attributes (no nicer way I know of to do this).. WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); wm.updateViewLayout(getDialog().getWindow().getDecorView(), getDialog().getWindow().getAttributes()); } }); 

呃,你正在创build自己的DialogFragment,你只需要重写这个方法。

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); return dialog; } 

结合这里的答案,我做了一个抽象类,在所有情况下工作:

 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 ); } } 

我知道这是旧post,但我的答案可能会帮助别人。

下面是在对话框中沉浸效果的hacky修复:

 public static void showImmersiveDialog(final Dialog mDialog, final Activity mActivity) { //Set the dialog to not focusable mDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); mDialog.getWindow().getDecorView().setSystemUiVisibility(setSystemUiVisibility()); mDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { //Clear the not focusable flag from the window mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); //Update the WindowManager with the new attributes WindowManager wm = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE); wm.updateViewLayout(mDialog.getWindow().getDecorView(), mDialog.getWindow().getAttributes()); } }); mDialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { mDialog.getWindow().getDecorView().setSystemUiVisibility(setSystemUiVisibility()); } } }); } public static int setSystemUiVisibility() { return View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; } 

这也适用于你的对话框片段的onDismiss方法。 在该方法中,再次调用与之相关联的活动的方法来设置全屏标志。

 @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); Logger.e(TAG, "onDismiss"); Log.e("CallBack", "CallBack"); if (getActivity() != null && getActivity() instanceof LiveStreamingActivity) { ((YourActivity) getActivity()).hideSystemUI(); } } 

在你的活动中添加这个方法:

 public void hideSystemUI() { // Set the IMMERSIVE flag. // Set the content to appear under the system bars so that the content // doesn't resize when the system bars hide and show. getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); }