Sign in Go Pro

Android MvRx Fundamentals

Make Your ViewModel Survive Configuration Changes

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next



MvRx makes handling configuration changes manually a thing of the past. By using the property delegate fragmentViewModel(), MvRx will handle configuration changes for you. No more parceling of data into bundles in onSaveInstanceState!
This lesson will also introduce the concept of invalidate() which will be called any time the state of any of your ViewModels change.




Hello Gabriel, Thanks for this awesome course.

It is very interesting for Fragments. Looks very easy.
About Activities, I found that I need my Activity to extend "BaseMvRxActivity()".
But what about state management? Is there similar delegation to replace this initialization similar to what you did for fragment?
private var viewModel = MainViewModel(MainState())

Thanks a lot.


I think I found something.
I need to replace above line with: private val viewModel: LoginViewModel by activityViewModel()
However, my new issue is, activityViewModel, cannot be imported by Android Studio.

Your feature code should be inside of a Fragment, not an Activity.

I would know how is the activity parent in order to allow the configuration changes in their fragments.
Thanks in advance.

After release v2.0.0, configigure your view-model factory at your Application file:

MvRx.viewModelConfigFactory = MvRxViewModelConfigFactory(applicationContext)

otherwise your application will crash.


Lessons in Android MvRx Fundamentals