user13201453
user13201453

Reputation:

Navigation: bundle in startDestination is null

I have HomeFragment, which includes RecyclerView. When I click on an item, I want to go from HomeFragment in navigation/home_navigation.xml to another DictionaryFragment, which is located in navigation/dictionary_navigation.xml, where DictionaryFragment is a startDestination.

I've created a new action to nested navigation/dictionary_navigation.xml graph because I need to open startDestination:

Nested graph

I don't know, how to transfer my data to DictionaryFragment correctly, but this:

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
}

Extensions

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

doesn't work, because bundle is null.

I also use SafeArgs plugin, but as I know, I can't use DictionaryFragmentArgs.fromBundle() with a startDestination.

How can I do this correctly?

Upvotes: 1

Views: 334

Answers (1)

Sinan Kara
Sinan Kara

Reputation: 26

I guess on DictionaryFragment you should try to fetch the data with getArguments()

Upvotes: 1

Related Questions