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

kim*_*oty 5 android screen-orientation kotlin

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.

我有代码可以在发生这种情况时重新进入全屏模式,但我想防止它从全屏模式开始。我想知道为什么会发生这种情况?

我还观察到,这只发生debuggable在 build.gradle 中为构建配置(发布或调试)的标志设置为 false 时

活动.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)
}     
Run Code Online (Sandbox Code Playgroud)

小智 0

Android 的系统 UI 处理有些复杂。我建议您将这些文件复制到您的应用程序:https ://gist.github.com/chrisbanes/73de18faffca571f7292

然后在你的活动中:


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()
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中做到了这一点,一切都很完美。我已经放弃尝试手动处理标志。