Fragments not added to backstack using Navigation Components

前端 未结 1 1322
广开言路
广开言路 2021-01-05 11:00

Information:

I\'m programmatically inserting a NavHostFragment for each feature of the app. Each NavHostFragment has it\'s own Navigation

相关标签:
1条回答
  • 2021-01-05 11:35

    Looks like a misunderstanding on my part (and a bug, also on my part).

    If you loop through the fragments in the childFragmentManager it only ever shows the top-most fragment for some reason.

    Example

    val navHostFragment = supportFragmentManager
                .findFragmentByTag(getString(R.string.NavHostFragmentTag)) as NavHostFragment
    val fragments = navHostFragment.childFragmentManager.fragments
    for(fragment in fragments){
        // Only prints a single fragment, no matter the backstack size
    }
    

    However, if you print the backstack size like this, you will get the correct answer.

    val navHostFragment = supportFragmentManager
                .findFragmentByTag(getString(R.string.NavHostFragmentTag)) as NavHostFragment
    val backstackCount = navHostFragment.childFragmentManager.backStackEntryCount
    println("backstack count: $backstackCount")
    

    At the end of the day this misunderstanding caused me to believe the fragments were not being added to the backstack. All is good.

    0 讨论(0)
提交回复
热议问题