I'm starting to learn about shared view model. Currently I've 3 fragments inside activity, 2 of them are inside nested navGraph.
I want to create shared navGraph viewModel scope to both of them, but I can't understand how and where I can initilaze the view model inside those fragments.
At all my past apps, I created Global view model
private lateinit var viewModel: MainViewModel
And then inside onCreateView I initlaize the viewModel like this -
viewModel = ViewModelProvider(this, Factory(requireActivity().application)).get(
MainViewModel::class.java)
How can I do the same with navGraph viewModel scope, If I want to share one view model with 2 fragments?
Currently I've this approach:
private val homeViewModel: HomeViewModel by navGraphViewModels(R.id.nested_navigation)
And It's work, but
A. I never saw viewModel intilazied right in the global variable
B. I can't pass variables inside factory with this approach