Is there a way to have a sharedViewModel for a Fragment's Scope?
See original GitHub issueI’m using Android Navigation Component, in a Single-Activity App, there is a ViewModel that I want to be re-created and it’s shared between some DialogFragments that compose this specific flow. I want to know if there is a way to have a sharedViewModel
tied to this FragmentScope
I would like to use a Scoped sharedViewModel
, I think that this will improve the library capacities
As an alternative, I would like to know if there is a way to clear a viewModel instance when the fragment is destroyed
koin-android
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:34 (4 by maintainers)
Top Results From Across the Web
Shared ViewModel Across Fragments
You will learn how to use a shared ViewModel to share data between the fragments of the same activity and new concepts like...
Read more >Sharing data between fragments using new architecture ...
These fragments can share a ViewModel using their activity scope to handle this communication. And shows a implementation example: public class ...
Read more >Shared ViewModel in Android
In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments...
Read more >Communicate Between Fragments Using ViewModel
Fragments can't and shouldn't talk to each other. Communication between fragments should be done via the associated activity . Fragments shouldn't have any...
Read more >Shared ViewModel - Explained | Android Studio Tutorial
Which make sense. In order to Share values from the same ViewModel instance between multiple Fragments, you need to scope ViewModel to an ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi, currently I’m using: Koin 2.1.0-beta-1 and this version of library have more elegant way to do it.
So, try to do like this :
To more deep understanding how it works, please check the package org.koin.androidx.viewmodel.ext.android of Koin library
Yes, you can set the scope like this:
sharedViewModel<ViewModel>(from = {//your scope})
In my case I have ViewPager, and i use the same viewModel for internal Fragments like:
val viewModel by sharedViewModel<MyViewModel>(from = { parentFragment!! })