Architecture ViewModel — a half baked solution?

Elye
Elye
Sep 30, 2019 · 3 min read

Architecture component is the way recommended by Google. It’s an MVVM approach, where it’s supposed to provide 3 solutions as below.

If you are unfamiliar with Architecture Component, you could refer to this simple tutorial, Architecture for Dummies.

LiveData and LifecycleObserver is great as it decouple the ViewModel from the Activity/Fragment, yet having them related.

The half baked ViewModel

The way ViewModel is created is through this ViewModelProviders factory class as below.

val model by lazy {
ViewModelProviders.of(this)[MyViewModel::class.java]
}

We do this instead of instantiating our own MyViewModel(), is so that it could outlive the Activity/Fragment owning it in onConfiguration change.

This may looks cool, as now when we rotate the device, all our data in the ViewModel is retained, without us needing to saveInstanceState.

Android does kill your process.

However, Android does have the liberty will kill your activity, due to various reason (e.g. if an incoming phone calls happens when memory is low etc). At this point, all your data in the ViewModel is now flushed away. Bye-bye.

To emulate this, just checkout the below blog.

To solve the problem, you’ll need the conventional approach of onSaveInstanceState of storing all your states before your activity/fragment got killed. And use the normal onRestoreInstanceState to restore it accordingly.

What’s the use of ViewModelProviders now?

By implementing your own onSaveInstanceState and onRestoreInstanceState, this itself also solve the onConfiguration change state lost.

Why do you still need to use ViewModelProvider anymore? Just instantiate MyViewModel() is have the same behavior.

Any if you see further, if you’re not using ViewModelProvider, you don’t even need to have your class implementing ViewModel() interface anymore. So you could create your native ViewModel POKO (Plain Old Kotlin Object 😅).

Back to square one 🤔

You could still use LivecycleOwner and LiveData independent from the Viewmodel.

Caveat

Perhaps the use of ViewModelProvider I could think of now is the ability to share the ViewModel across Fragments of the same activity as shared by https://developer.android.com/topic/libraries/architecture/viewmodel#sharing. It’s still nice as the Fragment doesn’t need to get the model from its Activity directly.

Let me know if you find out other use of ViewModel in Architecture Component.

Updated

After explore further, found a distinct different on ViewModel and SaveInstanceState. Check it out

Also for the Half-Baked Solution I complaint about, Google is coming up with a solution. Check out the below

I hope this post is helpful to you. You could check out my other topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store