GithubBrowserSample: demonstrate a way to communicate among ViewModels
See original GitHub issueHi!
I was looking at a way for one ViewModel
to access another ViewModel
and see if there any good practice to do so.
Unfortunately (unless I just didn’t find it) this sample doesn’t demonstrate a way to do this.
The ViewModel
guide talks about how you can “Share Data Between Fragments” with a “shared ViewModel
” but with no sample of how to share data between ViewModel
s.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
GithubBrowserSample: demonstrate a way to communicate ...
My way to share data between ViewModels that in the same activity or fragment: class GroupViewModels extends ViewModel { private A_ViewModel aVM ...
Read more >How to share view models between fragments using Google's ...
by viewModels() provides a ViewModel that is scoped to the individual Fragment. There's a separate by activityViewModels() that scopes the ...
Read more >ViewModels and LiveData: Patterns + AntiPatterns - Medium
The recommended way to communicate between ViewModels and Views is the observer pattern, using LiveData or observables from other libraries.
Read more >Encapsulating View State - Ryan Harter
Let's take a look at a simple approach to communication between the ViewModel and the view layer that allows us to have dead...
Read more >Android MVVM — A synchronized way of communication.
enqueueCommand(): is what the view models calls to put a command in its queue to be emitted in time. The “flags” here makes...
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 FreeTop 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
Top GitHub Comments
I’m not sure why you’re saying this is not the purpose of
ViewModel
. They allow to separate UI logic from other kinds of logic. In that sense, you could have one piece in oneViewModel
that depends on something that is in another one.For instance: let’s say you have a “search query” fragment where the user can fill some criteria. Then you have a results fragment. But you also want a “search summary” fragment, that shows some info about the search, including the query that comes from the “search query” fragment. To do that, the “search summary”
ViewModel
needs to observe some objects that are managed by the “search query”ViewModel
.[Question] is below:
Nobody has been answer… ((유∀유|||))
@BoD
Share data between ViewModels is hard! Not only conside
share data
but also have to manage the viewmodels’s lifecycle!My way to share data between ViewModels that in the same activity or fragment:
or
Way 2:
Warning:
No circular reference like below, please!