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
Autoplay

Up next

Previous

About

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.

Instructor

Links

Comments

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.

Update

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.
https://stackoverflow.com/q/55523891/513413

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

Hello Gabriel.
I have a trouble, my State is a Sealed Class and when I declare the viewModel as fragmentViewModel I get the next error:
Caused by: java.lang.IllegalStateException: Attempt to create the MvRx state class SearchState has failed. One of the following must be true: 1), 2) 3)

Can I use Sealed Class as MvRxState?

Thanks in advance.

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

>
You need to go PRO to post comments.

Lessons in Android MvRx Fundamentals