UPDATED ANSWER:
Set an OnSystemUiVisibilityChangeListener, force immersive mode when visibility is 0 (rather than 6).
if(android.os.Build.VERSION.SDK_INT >= 19) {
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if(visibility == 0) {
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);
}
}
});
}
OLD, NASTY ANSWER:
this is nasty, but it is a solution IFF you were showing an ActionBar:
- add an OnGlobalLayoutListener to the ViewTreeObserver of 'action_bar_container'.
- in the OnGlobalLayoutListener implementation, check if 'action_bar_container' visibility if GONE or not.
- when it moved from GONE to !GONE (and assuming you were in immersive mode) then force immersive mode again via the setSystemUiVisibility method.
if(android.os.Build.VERSION.SDK_INT >= 19) {
int actionBarContainerId = Resources.getSystem().getIdentifier("action_bar_container", "id", "android");
((ViewGroup)findViewById(actionBarContainerId)).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int actionBarContainerId = Resources.getSystem().getIdentifier("action_bar_container", "id", "android");
ViewGroup actionBarContainer = (ViewGroup) findViewById(actionBarContainerId);
if(actionBarContainer.getVisibility() == View.GONE) {
if(DEBUG) Log.d(TAG, "...PROBABLY IN IMMERSIVE MODE AND ALL IS GOOD!..");
} else {
if(DEBUG) Log.d(TAG, "...PROBABLY NO LONGER IN IMMERSIVE MODE, HEY..");
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);
}
}
});
}