Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ViewModelProviders

open class ViewModelProviders
kotlin.Any
   ↳ androidx.lifecycle.ViewModelProviders

Utilities methods for ViewModelStore class.

Summary

Nested classes

open

Factory which may create AndroidViewModel and ViewModel, which have an empty constructor.

Public constructors

Public methods

open static ViewModelProvider
of(@NonNull fragment: Fragment)

Creates a ViewModelProvider, which retains ViewModels while a scope of given fragment is alive.

open static ViewModelProvider
of(@NonNull activity: FragmentActivity)

Creates a ViewModelProvider, which retains ViewModels while a scope of given Activity is alive.

open static ViewModelProvider
of(@NonNull fragment: Fragment, @Nullable factory: ViewModelProvider.Factory?)

Creates a ViewModelProvider, which retains ViewModels while a scope of given fragment is alive.

open static ViewModelProvider
of(@NonNull activity: FragmentActivity, @Nullable factory: ViewModelProvider.Factory?)

Creates a ViewModelProvider, which retains ViewModels while a scope of given Activity is alive.

Public constructors

<init>

ViewModelProviders()

Deprecated: This class should not be directly instantiated

Public methods

of

@NonNull @MainThread open static fun of(@NonNull fragment: Fragment): ViewModelProvider

Deprecated: Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider#ViewModelProvider(ViewModelStoreOwner), passing in the fragment.

Creates a ViewModelProvider, which retains ViewModels while a scope of given fragment is alive. More detailed explanation is in ViewModel.

It uses the default factory to instantiate new ViewModels.

Parameters
fragment Fragment: a fragment, in whose scope ViewModels should be retained
Return
ViewModelProvider: a ViewModelProvider instance

of

@NonNull @MainThread open static fun of(@NonNull activity: FragmentActivity): ViewModelProvider

Deprecated: Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider#ViewModelProvider(ViewModelStoreOwner), passing in the activity.

Creates a ViewModelProvider, which retains ViewModels while a scope of given Activity is alive. More detailed explanation is in ViewModel.

It uses the default factory to instantiate new ViewModels.

Parameters
activity FragmentActivity: an activity, in whose scope ViewModels should be retained
Return
ViewModelProvider: a ViewModelProvider instance

of

@NonNull @MainThread open static fun of(@NonNull fragment: Fragment, @Nullable factory: ViewModelProvider.Factory?): ViewModelProvider

Deprecated: Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider#ViewModelProvider(ViewModelStoreOwner, Factory), passing in the fragment and factory.

Creates a ViewModelProvider, which retains ViewModels while a scope of given fragment is alive. More detailed explanation is in ViewModel.

It uses the given Factory to instantiate new ViewModels.

Parameters
fragment Fragment: a fragment, in whose scope ViewModels should be retained
factory Fragment: a Factory to instantiate new ViewModels
Return
ViewModelProvider: a ViewModelProvider instance

of

@NonNull @MainThread open static fun of(@NonNull activity: FragmentActivity, @Nullable factory: ViewModelProvider.Factory?): ViewModelProvider

Deprecated: Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider#ViewModelProvider(ViewModelStoreOwner, Factory), passing in the activity and factory.

Creates a ViewModelProvider, which retains ViewModels while a scope of given Activity is alive. More detailed explanation is in ViewModel.

It uses the given Factory to instantiate new ViewModels.

Parameters
activity FragmentActivity: an activity, in whose scope ViewModels should be retained
factory FragmentActivity: a Factory to instantiate new ViewModels
Return
ViewModelProvider: a ViewModelProvider instance