# ViewModelProviders

Artifact: [androidx.lifecycle:lifecycle-extensions](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/ViewModelProviders.java+class:androidx.lifecycle.ViewModelProviders) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

*** ** * ** ***


```
public class ViewModelProviders
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> Use the constructors for `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` directly.

Utilities methods for `https://developer.android.com/reference/androidx/lifecycle/ViewModelStore` class.

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders.DefaultFactory` **This class is deprecated.** Use ViewModelProvider.AndroidViewModelFactory <br /> |

| ### Public constructors |
|---|
| `[ViewModelProviders](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#ViewModelProviders())()` **This method is deprecated.** This class should not be directly instantiated <br /> |

| ### Public methods |
|---|---|
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | `@https://developer.android.com/reference/androidx/annotation/MainThread [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.FragmentActivity))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentActivity activity)` **This method is deprecated.** Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the activity. <br /> |
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | `@https://developer.android.com/reference/androidx/annotation/MainThread [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.Fragment))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment)` **This method is deprecated.** Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the fragment. <br /> |
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | `@https://developer.android.com/reference/androidx/annotation/MainThread [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.FragmentActivity,androidx.lifecycle.ViewModelProvider.Factory))( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentActivity activity, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory )` **This method is deprecated.** Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the activity and factory. <br /> |
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | `@https://developer.android.com/reference/androidx/annotation/MainThread [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.Fragment,androidx.lifecycle.ViewModelProvider.Factory))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory)` **This method is deprecated.** Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the fragment and factory. <br /> |

## Public constructors

### ViewModelProviders

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0)

```
public [ViewModelProviders](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#ViewModelProviders())()
```

> [!CAUTION]
> **This method is deprecated.**   
>
> This class should not be directly instantiated

## Public methods

### of

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

```
@MainThread
public static @NonNull ViewModelProvider [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.FragmentActivity))(@NonNull FragmentActivity activity)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the activity.

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`, which retains ViewModels while a scope of given Activity is alive. More detailed explanation is in `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

It uses the `https://developer.android.com/reference/androidx/activity/ComponentActivity#getDefaultViewModelProviderFactory()` to instantiate new ViewModels.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentActivity activity` | an activity, in whose scope ViewModels should be retained |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | a ViewModelProvider instance |

### of

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

```
@MainThread
public static @NonNull ViewModelProvider [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.Fragment))(@NonNull Fragment fragment)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the fragment.

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`, which retains ViewModels while a scope of given `fragment` is alive. More detailed explanation is in `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

It uses the `https://developer.android.com/reference/androidx/fragment/app/Fragment#getDefaultViewModelProviderFactory()` to instantiate new ViewModels.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment` | a fragment, in whose scope ViewModels should be retained |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | a ViewModelProvider instance |

### of

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

```
@MainThread
public static @NonNull ViewModelProvider [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.FragmentActivity,androidx.lifecycle.ViewModelProvider.Factory))(
    @NonNull FragmentActivity activity,
    @Nullable ViewModelProvider.Factory factory
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the activity and factory.

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`, which retains ViewModels while a scope of given Activity is alive. More detailed explanation is in `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

It uses the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` to instantiate new ViewModels.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/FragmentActivity activity` | an activity, in whose scope ViewModels should be retained |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory` | a `Factory` to instantiate new ViewModels |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | a ViewModelProvider instance |

### of

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.0.0) Deprecated in [2.2.0](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0)

```
@MainThread
public static @NonNull ViewModelProvider [of](https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders#of(androidx.fragment.app.Fragment,androidx.lifecycle.ViewModelProvider.Factory))(@NonNull Fragment fragment, @Nullable ViewModelProvider.Factory factory)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider, passing in the fragment and factory.

Creates a `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider`, which retains ViewModels while a scope of given `fragment` is alive. More detailed explanation is in `https://developer.android.com/reference/androidx/lifecycle/ViewModel`.

It uses the given `https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory` to instantiate new ViewModels.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment fragment` | a fragment, in whose scope ViewModels should be retained |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider.Factory factory` | a `Factory` to instantiate new ViewModels |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/ViewModelProvider` | a ViewModelProvider instance |