Keep activity in immersive mode

46 Views Asked by At

I am encountering difficulty maintaining immersive mode throughout my activity.

I have an activity consisting of various fragments. Within the initial (main) fragment, screen rotation is enabled. However, in the other fragment, screen rotation is disabled. Upon the activity's creation, the start fragment is displayed in immersive mode (fullscreen). When transitioning to the other fragment, the activity remains in immersive mode. However, upon navigating back to the start fragment, although the navigation bar is not visible, but the view does not return to fullscreen.

In the activity I have the following function which will be called when a fragment will resume (Inside th)

Activity:

fun setFullScreen() {
        this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
        val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
        windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
    }

Inside the fragments:

override fun onResume() {
        super.onResume()
        val activity = activity as MainActivity
        activity.setFullScreen()
        
    }

The problem is, as I wrote above, is that the navigation bar is hide, but the layout does not fit. The following picture shows the result after the return to the start fragment. Is it possible to resize the layout, so that the BottomNavigationView covers the navigation bar? The navigation bar is hide but the layout does not fit

1

There are 1 best solutions below

0
Mr T On

The problem was, that I used old code inside the Fragment

 private fun hideNavigationBar() {
        val flags =
            (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_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        requireActivity().window!!.decorView.setSystemUiVisibility(flags)
    }

    private fun showNavigationBar() {
        requireActivity().window!!.decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or View.SYSTEM_UI_FLAG_FULLSCREEN)
        requireActivity().window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }

By removing this code, it works fine.