Code Monkey home page Code Monkey logo

Comments (4)

ninadmg avatar ninadmg commented on July 17, 2024

I think the view-Models should communicate through their corresponding views.

from androidviewmodel.

DanielNovak avatar DanielNovak commented on July 17, 2024

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.

Blackhex avatar Blackhex commented on July 17, 2024

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.

Blackhex avatar Blackhex commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.