oldergod / android-architecture

MVI architecture Implementation of the ToDo app.
Apache License 2.0
669 stars 70 forks source link

ViewModel must never reference a view #38

Closed dmytrodanylyk closed 6 years ago

dmytrodanylyk commented 6 years ago

Hi,

The official Android documentation says:

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

I didn't dive deeply into MVI architecture and your code yet, but looks like View Model is observing events from Activty, which means it has a reference to activty.

Is it done intentional?

oldergod commented 6 years ago

Indeed, the ViewModel which would outlive the activity, could prevent the latter to be garbage collected and then would start leaking memory. When destroyed, the activity or fragment terminates its streams and unsubscribe from the ViewModel so no problem here.