Search code examples
androidandroid-architecture-navigation

Is there a way to use one fragment in landscape and others in portrait when using NavController?


I'm using the barcode scanner on one of my fragments and I need to rotate just that fragment to scan the barcode. I tried rotate by OnDestinationChangedListener in my single activity, but it seems to be so costly. How I can do this in a better way?


Solution

  • Yes you can Simply apply The Fragment Flags when you navigate to fragment Like This

    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      activity?.let {
        it.window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        it.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)
       }
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
         activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }