From 0a0c1d15083bed482eac9d49cda9435399024599 Mon Sep 17 00:00:00 2001 From: KeithYokoma Date: Mon, 22 Jun 2020 14:27:19 +0900 Subject: [PATCH] State saving with SavedStateHandle --- .../remotedata/android/sample/ui/main/MainFragment.kt | 5 +++++ .../remotedata/android/sample/ui/main/MainViewModel.kt | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainFragment.kt b/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainFragment.kt index 9007891..2a3f3ef 100644 --- a/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainFragment.kt +++ b/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainFragment.kt @@ -54,6 +54,11 @@ class MainFragment : Fragment(R.layout.main_fragment) { }) } + override fun onSaveInstanceState(outState: Bundle) { + viewModel.onSaveState() + super.onSaveInstanceState(outState) + } + override fun onDestroyView() { binding = null super.onDestroyView() diff --git a/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainViewModel.kt b/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainViewModel.kt index 7d7ac98..9717ac6 100644 --- a/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainViewModel.kt +++ b/remotedata_android_sample/src/main/java/com/mercari/remotedata/android/sample/ui/main/MainViewModel.kt @@ -46,6 +46,10 @@ class MainViewModel( ) } + fun onSaveState() { + savedStateHandle[STATE_MAIN_VIEW] = state.value + } + override fun onCleared() { disposables.clear() super.onCleared()