Does ViewModel survive Activity save and restore?

放肆的年华 提交于 2019-12-10 00:58:57

问题


Instances of the new ViewModel class can survive configuration changes if used in the following fashion:

mViewModel = ViewModelProviders.of(this).get(MyViewModel.class);

However, in addition to configuration changes, there is also a save-restore scenario when the entire application's process is being killed.

Will fields' values inside ViewModel be preserved during save-restore scenario?


Edit: based on the answer to this question, I wrote this article: Android ViewModel Architecture Component is Dangerous


回答1:


According to ViewModelProvider documentation (check get method), ViewModel is not preserved when app's process is killed:

The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed)

In addition check Ian Lake answer to similar question: https://medium.com/@ianhlake/you-are-correct-the-viewmodel-is-destroyed-if-your-process-is-killed-by-android-ef611fcd51e6

You are correct: the ViewModel is destroyed if your process is killed by Android. Just like before, you should use onSaveInstanceState() to store any data you must have to later recreate your Activity in the same state as before.

I also recommend reading Android ViewModel Architecture Component is Dangerous.




回答2:


It seems Google offers a solution for this now

Saved State module for ViewModel

UI State is usually stored or referenced in ViewModel objects, not activities; so using onSaveInstanceState() requires some boilerplate that this module can handle for you.

When the module is set up, ViewModel objects receive a SavedStateHandle object via its constructor. This is a key-value map that will let you write and retrieve objects to and from the saved state. These values will persist after the process is killed by the system and remain available via the same object.


Setup

implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-rc02' (November 7, 2019)


Usage

In order to set up a ViewModel to receive a SavedStateHandle you need to create them using a Factory that extends AbstractSavedStateVMFactory.

SavedStateViewModel vm = new ViewModelProvider(this, new SavedStateVMFactory(this))
        .get(SavedStateViewModel.class);

After that your ViewModel can have a constructor that receives a SavedStateHandle:


public class SavedStateViewModel extends ViewModel {
    private SavedStateHandle mState;

    public SavedStateViewModel(SavedStateHandle savedStateHandle) {
        mState = savedStateHandle;
    }
    ...
}

Storing and retrieving values

The SavedStateHandle class has the methods you expect for a key-value map:

  • get(String key)
  • contains(String key)
  • remove(String key)
  • set(String key, T value)
  • keys()


来源:https://stackoverflow.com/questions/44256250/does-viewmodel-survive-activity-save-and-restore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!