I was also faced with this issue. It seems there is no way to stop Android from showing the navigation bar again after the configuration changes.
What's worse, it is also not guaranteed when exactly the system UI will be restored. According to my tests on some devices the nav bar can reappear even after onWindowFocusChanged
and onResume
have been called.
The only reliable way I've found to prevent undesired UI reappearing is to add isInFullScreenMode
boolean flag and implement View.OnSystemUiVisibilityChangeListener
something like this:
@Override
public void onSystemBarsVisible() {
if (isInFullScreenMode) {
// If Android presented system bars
// but our app doesn't need them at this point
// just call hideSystemUi() again
hideSystemUi();
return;
}
}
@Override
public void onSystemBarsHidden() {
if (!isInFullScreenMode) {
// Similar technique as shown above
showSystemUi();
return;
}
}
Of course, sometimes on rotation we can see how the system bars quickly appear and disappear. But at least we can be sure that our app's UI state will be reliably restored.
Edit: To prevent possible confusion (as can be seen in the comments), I will clarify a couple of things:
onSystemBarsVisible
and onSystemBarsHidden
are custom methods which were defined in my app. You won't find them in Android frameworks;
- The
override
keywords are used here because these methods were part of a contract (interface);
- The app I used this functionality in is obsolete now. However, I still remember that the basic idea was as follows (snippet in Kotlin):
fun onSystemUiVisibilityChange(visibility: Int) {
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
// The system bars are visible
onSystemBarsVisible()
} else {
// The system bars are NOT visible.
onSystemBarsHidden()
}
}