4

I'm using Navigation fragment version 2.5.0. As release of version 2.4.0-alpha01, it's suppose to support back stack support for bottom navigation without writing any additional code. But my fragments are re-created everytime while I navigate with bottom navigation.

I'm using single graph, single activity Architecture.

Navigation dependencies

implementation 'androidx.navigation:navigation-fragment-ktx:2.5.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.0'

Nav-controller

val navHostFragment = supportFragmentManager.findFragmentById(
            R.id.nav_host_container
        ) as NavHostFragment
        navController = navHostFragment.navController

        // Setup the bottom navigation view with navController
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
        bottomNavigationView.setupWithNavController(navController)

        // Setup the ActionBar with navController and 3 top level destinations
        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.titleScreen, R.id.leaderboard,  R.id.register)
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
Fortray
  • 133
  • 1
  • 1
  • 8
  • 1
    Code is not enough to find the problem .. Follow [this sample](https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample/app) create Subgraphs for each tab . it should work.. – ADM Jul 08 '22 at 04:04
  • @ADM I have cloned that sample, that sample doesn't support backstack for bottom navigation view. Fragmetns are recreted with that sample repo. – Fortray Jul 08 '22 at 04:06
  • 1
    Recreated does not mean your state isn't restored - if your `onCreate()` is getting a non-null `savedInstanceState` Bundle, then you *are* getting restored. Are you [saving your state](https://developer.android.com/guide/fragments/saving-state)? – ianhanniballake Jul 08 '22 at 04:11
  • @ianhanniballake With navigation version 2.4.0-alpha01 onwards, it's suppose to save state without any additional code right? I'm using version 2.5.0, it's not saving state. – Fortray Jul 08 '22 at 04:15
  • Any state your fragment saves is restored automatically, yes. You still need to [actually have your fragment save its state](https://developer.android.com/guide/fragments/saving-state). If you've confirmed that your fragment is recreated with a non-null Bundle of `savedInstanceState`, then the Navigation part is working perfectly already and what you actually need is to create a new question that shares the code of one particular fragment that explains what state you expect to be restored that isn't being restored and we can help you do that correctly. – ianhanniballake Jul 08 '22 at 04:23
  • I am getting savedInstanceState non-null except for start destination, the first bottom tab fragment. Any idea how to get savedInstanceState non-null for start destination tab when we tap start destination tab again ? version 2.5.2 – user2836943 Sep 17 '22 at 11:54
  • @user2836943 I'm having the exact same problem here. The start destination never saves its instance state. – Soroush Lotfi Jan 18 '23 at 23:24

0 Answers0