Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

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

This class should not be directly instantiated

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

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

It uses ViewModelProvider.AndroidViewModelFactory 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

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

It uses ViewModelProvider.AndroidViewModelFactory 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

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

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