Erik Huizinga
1 min readOct 4, 2019

--

A misunderstanding is that a Fragment / Activity is a view. They actually are the presenters! They manage the actual Views that are shown. So it’s perfectly fine to use simple boolean logic in Fragment or Activity, e.g. to determine if a Button is visible or what the contents of some TextView should be.

The problem you’re having is probably that Fragment and Activity have a Lifecycle, so they have multiple responsibilities: manage their lifetime through callbacks (e.g. onResume) and manage their views. It’s no wonder people started moving the view management to separate presenters, but that’s just moving the problem out of your fragments and activities. That Lifecycle is also hard to unit test, so it makes sense to not put any logic in Fragment or Activity. However, keeping the logic trivial, e.g. by just using when on a finite state machine modeled by your ViewState, as written in this article by Shashank Gupta, there is nothing to test. Just make sure your Fragment / Activity observe the LiveData<ViewState> from your ViewModel and put the logic in there to dispatch the correct ViewState.

Fragment and Activity are presenters, ViewModel just presents ViewState decoupled from the Lifecycle of Fragment and Activity. But in turn, ViewModel doesn’t know about actual Views; that’s a job for Fragment / Activity.

--

--

No responses yet