Issue
I have a fragment container view to load more than three fragments, each fragment shared by ViewModel using navGraphViewModels
private val viewModel: HomeViewModel by navGraphViewModels(R.id.home_navigation_xml) { defaultViewModelProviderFactory }
How can we get the same instance of ViewModel inside parent activity?
Solution
I used the following method to solve my issue.
val viewModel by lazy {
binding?.homeFragmentContainerView?.findNavController()
?.getViewModelStoreOwner(R.id.home_navigation_xml)?.viewModelStore?.let {
ViewModelProvider(
it,
ViewModelProvider.AndroidViewModelFactory.getInstance(application)
).get(HomeViewModel::class.java)
}
}
Answered By - Bincy Baby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.