androidnavigationandroid-navigation-graph

android navigation graph configration change


I am trying to follow single activity pattern with android navigation component and my %99 of fragment are portrait but I need to make a new fragment can be portrait or landscape without adding new activity how can I achieve. I could't find any resource. is it possible ? if it is how ?


Solution

  • You can add NavController.OnDestinationChangedListener and set orientation according to the current fragment.

    Add this in your activity's onCreate:

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.your_nav_host_fragment) as NavHostFragment
    navHostFragment.navController..addOnDestinationChangedListener { _, destination, _ ->
        if (destination.id == R.id.destination_with_orientation) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
        } else {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }