Comments (4)
I think the view-Models should communicate through their corresponding views.
from androidviewmodel.
This dependes on the use-case. Each screen instance has it's ViewModel instance (if you open the same activity 3 times, each one will get it's unique ViewModel instance). So it's not possible to send data to viewmodels for screens that are not created yet.
Other cases should be handled with the standard android way - you can open a new Activity from the View (Activity/Fragment) and forward the data with a Bundle object (Intent extras/Fragment arguments). This Bundle is directly accessible in AbstractViewModel.onCreate(). There is no need to persist the data in database.
If you have multiple activities open and you would like to send an event from one ViewModel to others, then you could use Otto or a simple Broadcast. ViewModels can listen to these events directly and handle them.
from androidviewmodel.
Thank you for the response. Otto seem as a good alternative to not very readable Broadcasts. Do you know if Otto can send old messages (that was sent prior to registration) when the new and the only listener is just registered?
Add Bundles: They can be used when you want to communicate from the one view-model and the other that was created by the first one. But it does not solve the case when you want to communicate between two view-models that was created by the third one in case that one of the first two does not exists yet.
Possible solution would be that all the view-model instances would have some kind of instance identifier (a tag) and such a view-model would be singleton (in respect of its tag, not its class). There would also have to be an accessor method what would return or create view-model instance identified by the tag even if its fragment or activity does not exists yet. This would require modification to the android-view-model library so that view-models instances would be created by the library user in some kind of factory class or method.
PS: You mentioned that you would create a new Activity/Fragment from another Activity/Fragment. Isn't that breakage of the view-model-(controller) concept? Shouldn't the navigation logic (startActivity, etc.) be handled by the view-models?
from androidviewmodel.
To answer the first of my own questions: Otto producers may solve the issue with old messages receiving while just registering and it can also solve the problem with non-existing view-models too:
The source view-model registers itself as a producer and sends messages during its lifetime, the target view-model registers itself as a receiver and immediately calls the producer method to get the old value of the previously generated message - problem solved.
from androidviewmodel.
Related Issues (20)
- changing setModelView to onStart in sample HOT 1
- Communication between ViewModel and Model HOT 4
- More consistent naming convention HOT 5
- Android Studio custom template HOT 1
- Checking for PlayServices HOT 1
- ViewModel object creation - use Dagger 2 HOT 8
- View and ViewModel as a parent class HOT 2
- AVM in Activity issue HOT 1
- IView implementation for an recyclerview's item
- forget ViewModelBaseBindingActivity ha?
- Generic view model HOT 3
- Nested fragments support
- java.lang.IllegalStateException: ViewModel is not ready. Are you calling this method before Activity/Fragment onCreate? HOT 2
- jCenter deprecation
- Context for ViewModel HOT 2
- Other open source projects using AndroidViewModel.... HOT 1
- How to get or inject context in viewmodel... HOT 2
- transfer this rx subscription code from View to ViewModel.... HOT 1
- Please help : "cannot resolve symbol 'T' HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidviewmodel.