17

When I try to simulate configuration change in my app by enabling "Don't keep activities" in developer options every time I leave an activity and return, the ViewModel is recreated! Aren't ViewModels supposed to handle these situations?

I can handle this problem by saving my activity's state in onSaveInstanceState but then what's the point of using a ViewModel?

Gautam Surani
  • 1,136
  • 10
  • 21
Mostafa
  • 848
  • 3
  • 10
  • 21

1 Answers1

18

When I try to simulate configuration change in my app by enabling "Don't keep activities" in developer options every time I leave an activity and return, the ViewModel is recreated!

AFAIK, "don't keep activities" destroys activities when you navigate away from them. It does not simulate configuration changes.

On Android 8.1, the setting specifically states: "Destroy every activity as soon as the user leaves it".

Aren't ViewModels supposed to handle these situations?

The ViewModel system handles configuration changes. It does not handle activities being destroyed or processes being terminated.

To simulate a configuration change, change the configuration. For example, you could rotate the screen or change your locale.

I can handle this problem by saving my activity's state in onSaveInstanceState

Anything that can go into the saved instance state Bundle should go into the saved instance state Bundle, as that handles both configuration changes and process termination.

what's the point of using a ViewModel?

ViewModel is there for things that cannot go into the saved instance state Bundle, such as:

  • Big things (Bitmap of a photo)
  • Live things (LiveData, RxJava Observable, etc.)
  • Wrongly-typed things (you cannot put a Socket in a Bundle)
  • Things that are not really part of the "instance state" and should not be needed in case Android terminates the process, but you would like to have them around for a simple configuration change
  • And so on
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much. I also found this link very useful: https://developer.android.com/topic/libraries/architecture/saving-states – Mostafa Aug 09 '18 at 12:16
  • "AFAIK, 'don't keep activities' destroys activities when you navigate away from them. It does not simulate configuration changes." The onDestroy method of an Activity is called in both the cases, Configuration change and "Don't keep activities" action. Then what is the difference here? – ashwin mahajan Sep 18 '19 at 14:22
  • @ashwinmahajan: A configuration change recreates the activity and retains things like viewmodels. An activity being destroyed does neither of those things. – CommonsWare Sep 18 '19 at 21:59
  • does the behavior when opening intent camera (that our activity will be destroyed-recreated) because android will free memory, was not means for ViewModel? @CommonsWare or does we really need to use room as persistence data? – mochadwi Jun 01 '20 at 03:54
  • well what @Mostafa provides from official docs, that's not possible using ViewModel. It is indeed need to use persistance data storage – mochadwi Jun 01 '20 at 03:59
  • 1
    @mochadwi: Correct. `ViewModel` is purely held in RAM. When your process gets terminated, your `ViewModel` goes away, along with the rest of your objects. The saved instance state `Bundle` (or `SavedStateHandle` with newer versions of the `ViewModel` library) can help with short-term process termination scenarios like yours. Otherwise, save the data to files, databases, etc. – CommonsWare Jun 01 '20 at 11:04