I have experienced this symptom beginning from Android 9.0 (API-28).
When I observe these System_UI_FLAG_*
flags during initialization process with the decorView
being set OnSystemUiVisibilityChangeListener
, the underlying processes seem to be asynchronous. Sometimes when device rotation, after you set immersive flags in onCreate
or in onWindowFocusChanged
(or even in onResume
), one of the initialization process clears those flags afterwards.
By my own trial-and-error, I got two solutions.
- Use
OnSystemUiVisibilityChangeListener
to re-set immersive flags every time as soon as they are cleared. (almost same as @Siddarth_G's answer)
- Utilize
Handler.postDelay()
to make sure that my setting immersive flags after those initialization processes. (almost same as @r3pwn's https://mcmap.net/q/422407/-sticky-immersive-mode-disabled-after-soft-keyboard-shown pointed by @Luigi_Papardelle)
I didn't know which solution was ideal. However, the latter, @r3pwn's answer suggested it was from Google's official sample code. I thought the latter solution was quite primitive and analogue. So I didn't know whether it was ideal or not. But it doesn't seem that there is so advanced solution I've ever thought.
private static final long INITIAL_HIDE_DELAY = 1000L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
new Handler(getMainLooper())
.postDelayed(this::hideSystemUI, INITIAL_HIDE_DELAY);
}
private void hideSystemUI() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
);
}
This solution is just setting immersive flags once in onCreate
with sufficient delay time.