10

my viewmodel-savestate version is
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha03'

in version 1.0.0-alpha01 ,i can use

 MyViewModel myVM = new ViewModelProvider(this, new SavedStateVMFactory(this)).get(MyVM.class);

to create viewmodel with savestate, but in version 1.0.0-alpha03 ,

SavedStateVMFactory 

cant work , i need to use new SavedStateViewModelFactory but i dont konw what is the second params means, the code may look like this below:

        myVM = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(),xxxxxxx)).get(MyVM.class);

and i cant find any document about this in android developer website, sad

treesAreEverywhere
  • 3,722
  • 4
  • 28
  • 51
xizz
  • 103
  • 1
  • 4
  • You don't need the gradle dependency `viewmodel-savedstate`. **androidx.lifecycle:lifecycle-livedata-ktx** does this already. – IgorGanapolsky May 31 '20 at 14:56

1 Answers1

18

As per the SavedStateRegistryOwner documentation, both Fragment, and AppCompatActivity implement SavedStateRegistryOwner, so you can just pass in this:

 myVM = new ViewModelProvider(this,
     new SavedStateViewModelFactory(getApplication(), this))
     .get(MyVM.class);

Just make sure you're using AppCompat 1.1.0, which is when AppCompatActivity (and its base class, FragmentActivity and ComponentActivity) started to implement SavedStateRegistryOwner.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Great! If this answered your question, make sure to hit the check box next to the answer to accept it and remove the question from the queue of unanswered questions. – ianhanniballake Sep 08 '19 at 03:08
  • sorry to apologize, ask one more question, when i restart the phone , the save-state can't work , it's sth wrong in my code``` public SavedStateHandle handle; public MyVM(SavedStateHandle savedStateHandle) { this.handle = savedStateHandle; } public MutableLiveData getNumbers() { if (!this.handle.contains(MainActivity.KEY_NUM)) { this.handle.set(MainActivity.KEY_NUM, 0); } return this.handle.getLiveData(MainActivity.KEY_NUM); } ``` – xizz Sep 08 '19 at 03:12
  • Saved state doesn't persist across reboots - you'd need to [persist the data to disk](https://developer.android.com/guide/topics/data/data-storage) via SharedPreferences, a database, etc. – ianhanniballake Sep 08 '19 at 03:40
  • i got another problem in [https://stackoverflow.com/questions/57926170/why-savedstatehandle-setjava-lang-string-java-lang-object-on-a-null-object-re] can u help me ,plz!!!! – xizz Sep 13 '19 at 15:13
  • @ianhanniballake This is convenient, but are there any disadvantages to using a different `SavedStateViewModelFactory` in each Fragment, as opposed to one per Activity (or per app, if that's possible)? – big_m Nov 12 '19 at 23:04
  • @big_m - no, there are no disadvantages. Doing it any other way will give you the wrong result (the saved state won't be added to the right `SavedStateRegistry` and hence, won't be removed when the Fragment is removed, etc, etc) – ianhanniballake Nov 13 '19 at 00:03