导航:startDestination 中的包为空

Navigation: bundle in startDestination is null

我有 HomeFragment,其中包含 RecyclerView。当我单击一个项目时,我想从 navigation/home_navigation.xml 中的 HomeFragment 转到位于 navigation/dictionary_navigation.xml 中的另一个 DictionaryFragment,其中 DictionaryFragment 是 startDestination

我为嵌套 navigation/dictionary_navigation.xml 图创建了一个新操作,因为我需要打开 startDestination:

我不知道如何将我的数据正确传输到 DictionaryFragment,但是这个:

HomeFragment

wordPackAdapter = PackAdapter(context, Utils.dictionaryPacks) {
    val bundle = Bundle().apply {
        putInt("packId", it.index)
    }
    navigate(R.id.action_homeFragment_to_dictionary_navigation, bundle)
}

DictionaryFragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val bundle = requireActivity().intent.extras <---- null
}

扩展

fun BaseFragment.navigate(
    resId: Int,
    bundle: Bundle? = null,
    navOptions: NavOptions? = null
) {
    NavHostFragment.findNavController(this).navigate(resId, bundle, navOptions)
}

不起作用,因为 捆绑包为空

我也使用 SafeArgs 插件,但据我所知,我不能将 DictionaryFragmentArgs.fromBundle() 与 startDestination 一起使用。

我怎样才能正确地做到这一点?

我想在 DictionaryFragment 上你应该尝试使用 getArguments()

获取数据