Issue
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:
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?
Solution
I guess on DictionaryFragment you should try to fetch the data with getArguments()
Answered By - Sinan Kara
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.