SavedStateVMFactory

class SavedStateVMFactory : AbstractSavedStateVMFactory
androidx.lifecycle.AbstractSavedStateVMFactory
   ↳ androidx.lifecycle.SavedStateVMFactory

ViewModelProvider.Factory that can create ViewModels accessing and contributing to a saved state via SavedStateHandle received in a constructor. If defaultArgs bundle was passed in SavedStateVMFactory(Fragment, Bundle) or SavedStateVMFactory(FragmentActivity, Bundle), it will provide default values in SavedStateHandle.

If ViewModel is instance of AndroidViewModel, it looks for a constructor that receives an Application and SavedStateHandle (in this order), otherwise it looks for a constructor that receives SavedStateHandle only.

Summary

Public constructors

<init>(@NonNull fragment: Fragment)

Creates SavedStateVMFactory.

<init>(@NonNull fragment: Fragment, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

<init>(@NonNull activity: FragmentActivity)

Creates SavedStateVMFactory.

<init>(@NonNull activity: FragmentActivity, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

<init>(@NonNull application: Application, @NonNull owner: SavedStateRegistryOwner, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

Protected methods

T
create(@NonNull key: String, @NonNull modelClass: Class<T>, @NonNull handle: SavedStateHandle)

Inherited functions

Public constructors

<init>

SavedStateVMFactory(@NonNull fragment: Fragment)

Creates SavedStateVMFactory.

ViewModel created with this factory can access to saved state scoped to the given fragment.

Parameters
fragment Fragment: scope of this fragment will be used for state saving

<init>

SavedStateVMFactory(@NonNull fragment: Fragment, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

ViewModel created with this factory can access to saved state scoped to the given fragment.

Parameters
fragment Fragment: scope of this fragment will be used for state saving
defaultArgs Fragment: values from this Bundle will be used as defaults by SavedStateHandle if there is no previously saved state or previously saved state miss a value by such key.

<init>

SavedStateVMFactory(@NonNull activity: FragmentActivity)

Creates SavedStateVMFactory.

ViewModel created with this factory can access to saved state scoped to the given activity.

Parameters
activity FragmentActivity: scope of this activity will be used for state saving

<init>

SavedStateVMFactory(@NonNull activity: FragmentActivity, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

ViewModel created with this factory can access to saved state scoped to the given activity.

Parameters
activity FragmentActivity: scope of this activity will be used for state saving
defaultArgs FragmentActivity: values from this Bundle will be used as defaults by SavedStateHandle if there is no previously saved state or previously saved state misses a value by such key.

<init>

SavedStateVMFactory(@NonNull application: Application, @NonNull owner: SavedStateRegistryOwner, @Nullable defaultArgs: Bundle?)

Creates SavedStateVMFactory.

ViewModel created with this factory can access to saved state scoped to the given activity.

Parameters
application Application: an application
owner Application: SavedStateRegistryOwner that will provide restored state for created ViewModels
defaultArgs Application: values from this Bundle will be used as defaults by SavedStateHandle if there is no previously saved state or previously saved state misses a value by such key.

Protected methods

create

@NonNull protected fun <T : ViewModel!> create(@NonNull key: String, @NonNull modelClass: Class<T>, @NonNull handle: SavedStateHandle): T