Search code examples
androidandroid-fragmentsandroid-fragmentactivityandroid-jetpack

Android NavigationUI Fragment in Fullscreen Mode


I am exploring Google's Sunflower sample Jetpack app (https://github.com/googlesamples/android-sunflower). The NavGraph is defined in a navigation xml file that has a single Activity and numerous Fragments. The main Activity layout has an AppBar which is shared by all the fragments. I would like to add another Fragment to the NavGraph that displays an image in Fullscreen mode. The Android documentation shows the following code to enable Fullscreen mode:

 private fun hideSystemUI() {
            window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
            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_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

I call hideSystemUI in the new Fragment's onResume(), and it is not working. The Fragment still shows the main AppBar and navigation that were set up in MainActivity. I've tried a variety of other solutions from S.O. threads, but most seem to be based on the standard Activity/Fragment pattern that I've had success with in my pre-Jetpack apps. The new layout contains a FrameLayout with an ImageView. My new Fragment looks like this:

class FullScreenFragment: Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val binding = DataBindingUtil.inflate<FragmentFullscreenBinding>(
                inflater, R.layout.fragment_fullscreen, container, false)
        return binding.root
    }

    override fun onResume() {
        super.onResume()
        hideSystemUI()
    }

    private fun hideSystemUI() {
                    activity!!.window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                                    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_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
}

Is there a way to enable Fullscreen mode for one fragment? I haven't changed Google's code at all, except to add the new Fragment to the NavGraph, but I can post additional code here if necessary. I'm thinking that this may have to be managed through MainActivity, but I'm not sure. Thanks in advance!


Solution

  • The Fragment still shows the main AppBar and navigation that were set up in MainActivity.

    Those are not part of the system UI, which is what hideSystemUI() is trying to control. If you want to hide widgets, update the visibility property (e.g., findViewById(R.id.appbar).visibility = View.GONE).