Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you detect whether an application is in immersive mode in Fragment.onCreateView?

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?

like image 984
Mark Avatar asked Mar 05 '14 21:03

Mark


2 Answers

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);
    }
}
like image 185
RyPope Avatar answered Nov 14 '22 23:11

RyPope


@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.

like image 39
Larry Schiefer Avatar answered Nov 14 '22 22:11

Larry Schiefer