question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Immersive Mode not working after onWindowFocusChanged()

See original GitHub issue

In AndroidApplication.java:

    protected void hideStatusBar (boolean hide) {
        if (!hide || getVersion() < 11) return;

        View rootView = getWindow().getDecorView();

        try {
            Method m = View.class.getMethod("setSystemUiVisibility", int.class);
            if (getVersion() <= 13) m.invoke(rootView, 0x0);
            m.invoke(rootView, 0x1);
        } catch (Exception e) {
            log("AndroidApplication", "Can't hide status bar", e);
        }
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        useImmersiveMode(this.useImmersiveMode);
        hideStatusBar(this.hideStatusBar);
        if (hasFocus) {
            this.wasFocusChanged = 1;
            if (this.isWaitingForAudio) {
                this.audio.resume();
                this.isWaitingForAudio = false;
            }
        } else {
            this.wasFocusChanged = 0;
        }
    }

    @TargetApi(19)
    @Override
    public void useImmersiveMode (boolean use) {
        if (!use || getVersion() < Build.VERSION_CODES.KITKAT) return;

        View view = getWindow().getDecorView();
        try {
            Method m = View.class.getMethod("setSystemUiVisibility", int.class);
            int code = 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;
            m.invoke(view, code);
        } catch (Exception e) {
            log("AndroidApplication", "Can't set immersive mode", e);
        }
    }

Method onWindowFocusChanged() contains a bug, useImmersiveMode() should be executed after hideStatusBar() and not before. As it is now, hideStatusBar() overwrites the changes to SystemUiVisibility done by useImmersiveMode(), and this makes Immersive Mode not working after a call to onWindowFocusChanged()

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorentMassoncommented, May 13, 2017

I struggled a while with this immersive mode, which was working randomly, especially on slow devices. I discovered this stackoverflow answer which refers to a google sample The setSystemUiVisibility call should be manager by a handler and delayed by 300ms. Also I think hideStatusBar() and useImmersive() calls should be merged in a single setSystemUiVisibility() call, because otherwise, on focus change, the status bar gets shown for a very short time (which causes a very unpleasant blink effect). I finally set config.hideStatusBar to false.

0reactions
shatterblastcommented, Apr 27, 2020

It works for me. If no one has a complaint, can we close this, please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Return to immersive mode after closing the keyboard on Android
I used handlers to detect user inactivity and hide system ui thereafter. It automatically detect if user is not interacting on screen then...
Read more >
Moving forward with the example | Mastering Android Game ...
Immersive mode hides the Status and Navigation bars completely. It is designed, as the name indicates, for fully-immersive experiences, which means games mostly ......
Read more >
Activity - Android Developers
onRestart(), Called after your activity has been stopped, prior to it being started again. ... Adjust the current immersive mode setting.
Read more >
Android Immersive fullscreen mode - Qt Forum
I have this class in my app and tried doing this. The method onWindowFocusChanged() was not called. Here I found solution with QtActivity.java ......
Read more >
react-native-immersive - npm
Start using react-native-immersive in your project by running `npm ... and Immersive Full-Screen Mode is first introduced since Android 4.4 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found