Overflow Menu click disabling Immersive mode - Android 4.4 Kitkat

In K, the overflow menu is a separate window that takes focus, and therefore drives the current system UI flags.

However, this only comes up if you are trying to show the action bar when the status bar is hidden, which is discouraged. For contextual menus outside of the action bar, you can use PopupWindow instead (with PopupWindow you can set the system UI flags yourself as needed).


I am not clear on how to set the system UI flags for the PopupWindow. I tried something like this:

PopupMenu popupMenu = new PopupMenu(mainHandle, view) {
   @Override
   public void 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 |
           View.SYSTEM_UI_FLAG_FULLSCREEN |
           View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
       super.show();
}