my viewmodel-savestate version isimplementation '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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With