Coder Social home page Coder Social logo

Comments (8)

FrantisekGazo avatar FrantisekGazo commented on July 19, 2024

@dozd You can also try Blade mvp module - it allows you to inject Presenters via Dagger2.

from androidviewmodel.

dominikmicuta avatar dominikmicuta commented on July 19, 2024

@dozd what about change of dependencies in component? If you inject them to ViewModel during creation through constructor, they can be no longer valid when activity subcomponent is recreated.
You load ViewModel with old dependencies from previous Subcomponent.
What about creating ViewModel without dependencies and reinject it when he is loaded from ViewModelProvider?

from androidviewmodel.

dozd avatar dozd commented on July 19, 2024

@micutad I can't imagine that dependencies have "shorter life" than ViewModel. One ViewModel cannot be shared through various Subcomponents as it's bound to the specific fragment that is in specific subcomponent.

Creating class without dependencies and injecting them during life-time of the class is anti-pattern in terms of mutability of the class. Dependency injection is about creating class with their dependencies easily not about managing them during the time of the class.

from androidviewmodel.

dominikmicuta avatar dominikmicuta commented on July 19, 2024

@dozd
If you have activity subcomponent than this subcomponent is recreated on config change because old activity is destroyed with current subcomponent, and new activity is created with new subcomponent.

If ViewModel is injected with this subcomponent and saved on config change then all dependencies are invalid after loading retained ViewModel because new activity have new subcomponent with new dependencies.

Not sure if Im explaining this clearly but in my case I have dependencies in ViewModel that are from Activity subcomponent and not from Application component.

from androidviewmodel.

dozd avatar dozd commented on July 19, 2024

How's your ViewModel living longer than activity? That sounds weird.

from androidviewmodel.

dominikmicuta avatar dominikmicuta commented on July 19, 2024

from androidviewmodel.

dozd avatar dozd commented on July 19, 2024

But for one fragment. And fragment does not live longer than activity as well.

from androidviewmodel.

dominikmicuta avatar dominikmicuta commented on July 19, 2024

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.