In a fragment I have implemented a GestureDetector.SimpleOnGestureListener
so that I can enter/exit immersive mode when onSingleTapUp
is detected.
A FragmentStatePagerAdapter
is used to move between these fragments on swipe left/right. If you enter immersive mode then swipe to a new fragment the UI remains in immersive mode.
However, in the onCreateView
method of the new fragment I need to detect whether the UI is in immersive mode to when creating my listener.
I have tried calling getSystemUiVisibility()
on the new view but this returns SYSTEM_UI_FLAG_VISIBLE
.
Is there a method for detecting whether the application is in immersive mode from any view or fragment regardless of whether that initiated the transition to immersive mode?
If anyone is looking for a more in-depth answer. To check if the window is in immersive vs non-immersive you can do something use:
(getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_IMMERSIVE) == View.SYSTEM_UI_FLAG_IMMERSIVE
An example of it's usage for swapping between immersive and normal:
private void toggleImmersive() {
if ((getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_IMMERSIVE) == View.SYSTEM_UI_FLAG_IMMERSIVE) {
getWindow().getDecorView().setSystemUiVisibility( // Go fullscreen
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
getWindow().getDecorView().setSystemUiVisibility( // Go immersive
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);
}
}
@Mark, it sounds like you may have gotten it resolved based on my previous comment: use a View
owned by the Activity
to call getSystemUiVisibility()
rather than the Fragment
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With