Prevent fullscreen exit (window flag clear?) on orientation change

Viewed 141

I have a fullscreen activity, with no configChanges set in AndroidManifest.xml so that the system recreates the activity on orientation change. Everything works fine here, except that intermittently, an orientation change seems to cause the app to exit fullscreen mode and bring the system ui back in.

I have code to re-enter fullscreen when this happens, but I'd like to prevent it from coming out of fullscreen to begin with. I'm wondering why this may be happening?

I have also observed that this only happens when the debuggable flag is set to false in build.gradle for the build config (release or debug)

Activity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // initial fullscreen mode before content loads
    enterFullscreenMode()

    window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
        if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
            // not full screen
            Handler().postDelayed({
                enterFullscreenMode()
            }, 800L)
        }
    }
}

private fun enterFullscreenMode() {
    window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
}     
1 Answers

Android's system UI handling is somewhat complex. I would suggest you copy these files to your app: https://gist.github.com/chrisbanes/73de18faffca571f7292

Then in your activity:


class MyActivity : AppCompatActivity {

    /** System UI helper used to hide the navigation bar. */
    private lateinit var systemUiHelper: SystemUiHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Keep full screen on.
        window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        systemUiHelper = SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE, SystemUiHelper.FLAG_IMMERSIVE_STICKY)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            systemUiHelper.hide()
        }
    }

}

I did this in my app and all works perfectly. I've quit trying to deal with the flags manually.

Related