Navigation component: Retain same fragment after orientation change

Viewed 317

as I said in the title, I would like to keep my current fragment after rotating the phone. I could actually make it happen by adding

android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

in the Manifest. The problem is that my layout configuration is lost every time. What am I doing wrong? How can I fix it?

I'm using Jetpack Navigation component. Exist a way to navigateToCurrentFragment()?

Edit: Part of onCreate() from Activity()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityBinding.inflate(layoutInflater)
    setContentView(binding.root)
    

    val graphInflater = (supportFragmentManager.findFragmentById(R.id.mNavHost) as NavHostFragment).navController.navInflater
    val navGraph = graphInflater.inflate(R.navigation.services_nav_graph)

    navGraph.startDestination = R.id.FirstFragment
    (supportFragmentManager.findFragmentById(R.id.mNavHost) as NavHostFragment).navController.graph = navGraph

}
0 Answers
Related