Guess I'm missing something obvious here but... I'm storing data in uiModel
in the DiaryViewModel class, and since I use architecture components I'm expecting the data to be retained through screen rotation - but it doesn't. I'm blind to why.
Here's a stripped down fragment
class DiaryFragment: Fragment() {
private lateinit var viewModel: DiaryViewModel
override onCreateView(...) {
viewModel = ViewModelProviders.of(this).get(DiaryViewModel::class.java)
viewModel.getModel().observe(this, Observer<DiaryUIModel> { uiModel ->
render(uiModel)
})
}
}
And the corresponding view model.
class DiaryViewModel: ViewModel() {
private var uiModel: MutableLiveData<DiaryUIModel>? = null
fun getModel(): LiveData<DiaryUIModel> {
if (uiModel == null) {
uiModel = MutableLiveData<DiaryUIModel>()
uiModel?.value = DiaryUIModel()
}
return uiModel as MutableLiveData<DiaryUIModel>
}
}
Can any one see what's missing in this simple example? Right now, uiModel
is set to null
when rotating the screen.
Lifecycles
(though not certain that would be cause of issue you're seeing) – Corpuzactivity
in toViewModelProviders.of()
but you'd also need to check that that makes sense for your particular use case (example of doing that is shown in developer.android.com/topic/libraries/architecture/…) – Corpuz