Closed RothAndrew closed 7 years ago
I finally figured it out after about 5 hours. I have no idea whether this is the right way to do it or not so I'm not going to close the issue until someone else takes a look at it.
My solution utilizes Parceler. To add Parceler to your project add the following line to the dependencies section in build.gradle.
compile 'org.parceler:parceler-api:1.1.5'
In PeopleActivity
I added private List<People> peopleList;
and changed onCreate() to this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataBinding();
setSupportActionBar(peopleActivityBinding.toolbar);
setupListPeopleView(peopleActivityBinding.listPeople);
if (savedInstanceState != null){
peopleList = Parcels.unwrap(savedInstanceState.getParcelable("peopleList"));
if (peopleList != null){
loadData(peopleList);
peopleViewModel.onDataLoaded();
}
}
}
I added an onSaveInstanceState
override to PeopleActivity
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelable("peopleList", Parcels.wrap(peopleList));
super.onSaveInstanceState(outState);
}
I added void onDataLoaded();
to the PeopleViewModelContract.ViewModel
and implemented it in PeopleViewModel
@Override
public void onDataLoaded(){
peopleProgress.set(View.GONE);
peopleLabel.set(View.GONE);
peopleList.set(View.VISIBLE);
}
@RothAndrew you probably should pass savedInstanceState bundle to a view model, either with viewModel = new ViewModel(savedInstanceState) or via some other method. If feels wrong and probably is to invoke peopleViewModel.onDataLoaded within activity.
@erikcaffrey i could implement solution for saving state when rotating, although i am not entirely familiar with this project (just came from google search results page :) )
How do you persist the list of people when the activity is destroyed and recreated when the device is rotated?