3

This is more of a conceptual question.

Android recently introduced the concept of handling the state during process death inside viewModel rather than savedInstanceState which previously used to be in Activity or Fragment.

My question is how do I test this process death? If I rightly understand the documentation, the Process death happens to be Android killing the app due to full memory when app is no longer used in background.

Also will the viewModel save the state when user kills the app from the RAM? So that I can retrieve the state as the user opens the app again.

Ma2340
  • 647
  • 2
  • 17
  • 34
  • If you are running debug app build, you can stop app from Android Studio (red square) and then resume it by opening it on your device. There are other ways accessible from developer options. – ror Feb 18 '20 at 18:07

2 Answers2

3

enter image description here

  1. Enable `Don't keep activities'
  2. Launch your app
  3. Tap the home button
  4. Your app will be killed immediately (Activity.onDestroy called)
CHAN
  • 1,518
  • 18
  • 16
  • Thank you... Will the state be saved when the app is killed from the RAM? – Ma2340 Mar 11 '20 at 18:49
  • @Maria not by default. you need to at least get/set the saved state just like a bundle, check this out: https://medium.com/androiddevelopers/viewmodels-with-saved-state-jetpack-navigation-data-binding-and-coroutines-df476b78144e#5c5b SavedStateHandle.getLiveData is the better option thou I would say, get/set the LiveData value (kotlin `data class` + `@Parcelize`) as usual and the data is saved/restored like transparently. – CHAN Mar 11 '20 at 20:10
0

Below Steps helps in testing the intelligence of View Model Saved State Library

Step 1: After completing the source code changes, build and run the APP.
Step 2: Ensure UI of Activity/Fragment has data populated in it.
Step 3: Move the App to background by clicking Home Button.
Step 4: Now Open the Logcat Tool window and Terminate the APP.
Step 5: Relaunch the APP from the Device or Emulator.
Step 6: You should see all the data pre-populated in UI.
Anandaraja_Srinivasan
  • 568
  • 1
  • 10
  • 25