I have one view model which should be shared between two fragments. The two fragments are in a nav graph and in each fragment I do this to reference the view model:
private val viewModel: MyViewModel by navGraphViewModels(R.id.my_nav_graph)
As I understand, this will keep the lifecycle of the view model with the nav graph, and it's all working great. The two fragments share data from the view model.
But I noticed that when I switch from the first fragment to the second, the init method in the view model is running again and if I print out the name of the viewmodel, I get one instance when the first fragment loads and another name when the second one loads (similar to this format MyViewModel@e7e7690).
Now, the view model works fine for the two fragments, the data is shared and all, but I'm concerned that there might be another view model lingering in the background which will not be removed correctly. In the project the view model is subscribing to a service on creation and unsubscribing onDestroyed(), but onDestroyed() is only called once when the fragments are destroyed, meaning that one of the subscribers are still registered, hence it's a problem if the view model are created twice and not handled properly.