blob: 96eda057f50d93ad903b760a5ea1240bc9d0a226 [file] [log] [blame]
<html devsite="true">
<head>
<title>ViewModelProvider</title>
{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %}
{% include "_shared/_reference-head-tags.html" %}
</head>
<body>
<div id="metadata-info-block"></div>
<h1>ViewModelProvider</h1>
<p>
<pre>public class <a href="/reference/androidx/lifecycle/ViewModelProvider.html">ViewModelProvider</a></pre>
</p>
<hr>
<p>An utility class that provides <code>ViewModels</code> for a scope.</p>
<p>Default <code>ViewModelProvider</code> for an <code>Activity</code> or a <code>Fragment</code> can be obtained by passing it to the constructor: <code>ViewModelProvider(myFragment)</code></p>
<h2>Summary</h2>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%"><h3>Nested types</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td>
<div><code>public class <a href="/reference/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory.html">ViewModelProvider.AndroidViewModelFactory</a> extends <a href="/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.html">ViewModelProvider.NewInstanceFactory</a></code></div>
<p><code><a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">Factory</a></code> which may create <code><a href="/reference/androidx/lifecycle/AndroidViewModel.html">AndroidViewModel</a></code> and <code><a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a></code>, which have an empty constructor.</p>
</td>
</tr>
<tr>
<td>
<div><code>public static class <a href="/reference/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory.Companion.html">ViewModelProvider.AndroidViewModelFactory.Companion</a></code></div>
</td>
</tr>
<tr>
<td>
<div><code>public interface <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a></code></div>
<p>Implementations of <code>Factory</code> interface are responsible to instantiate ViewModels.</p>
</td>
</tr>
<tr>
<td>
<div><code>public static class <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.Companion.html">ViewModelProvider.Factory.Companion</a></code></div>
</td>
</tr>
<tr>
<td>
<div><code>public class <a href="/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.html">ViewModelProvider.NewInstanceFactory</a> implements <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a></code></div>
<p>Simple factory, which calls empty constructor on the give class.</p>
</td>
</tr>
<tr>
<td>
<div><code>public static class <a href="/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.Companion.html">ViewModelProvider.NewInstanceFactory.Companion</a></code></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%"><h3>Public constructors</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td>
<div><code><a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)">ViewModelProvider</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>&nbsp;owner)</code></div>
<p>Creates <code>ViewModelProvider</code>.</p>
</td>
</tr>
<tr>
<td>
<div><code><a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>&nbsp;owner,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>&nbsp;factory<br>)</code></div>
<p>Creates <code>ViewModelProvider</code>, which will create <code>ViewModels</code> via the given <code>Factory</code> and retain them in a store of the given <code>ViewModelStoreOwner</code>.</p>
</td>
</tr>
<tr>
<td>
<div><code><a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a>&nbsp;store,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>&nbsp;factory,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/viewmodel/CreationExtras.html">CreationExtras</a>&nbsp;defaultCreationExtras<br>)</code></div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%"><h3>Public methods</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T</code></td>
<td>
<div><code>@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>&lt;T&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">get</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass)</code></div>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
</td>
</tr>
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T</code></td>
<td>
<div><code>@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>&lt;T&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(kotlin.String,java.lang.Class)">get</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/String.html">String</a>&nbsp;key,&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass)</code></div>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%"><h3>Extension functions</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>final @<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> VM</code></td>
<td>
<div><code>@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>&lt;VM&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProviderGetKt.html">ViewModelProviderGetKt</a>.<a href="/reference/androidx/lifecycle/ViewModelProvider.html#(androidx.lifecycle.ViewModelProvider).get()">get</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.html">ViewModelProvider</a>&nbsp;receiver<br>)</code></div>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="list">
<h2>Public constructors</h2>
<div class="api-item"><a name="ViewModelProvider-androidx.lifecycle.ViewModelStoreOwner-"></a><a name="viewmodelprovider"></a>
<h3 class="api-name" id="ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)">ViewModelProvider</h3>
<pre class="api-signature no-pretty-print">public&nbsp;<a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)">ViewModelProvider</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>&nbsp;owner)</pre>
<p>Creates <code>ViewModelProvider</code>. This will create <code>ViewModels</code> and retain them in a store of the given <code>ViewModelStoreOwner</code>.</p>
<p>This method will use the <code><a href="/reference/androidx/lifecycle/HasDefaultViewModelProviderFactory.html#getDefaultViewModelProviderFactory()">default factory</a></code> if the owner implements <code><a href="/reference/androidx/lifecycle/HasDefaultViewModelProviderFactory.html">HasDefaultViewModelProviderFactory</a></code>. Otherwise, a <code><a href="/reference/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.html">NewInstanceFactory</a></code> will be used.</p>
</div>
<div class="api-item"><a name="ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner, androidx.lifecycle.ViewModelProvider.Factory)"></a><a name="ViewModelProvider-androidx.lifecycle.ViewModelStoreOwner-androidx.lifecycle.ViewModelProvider.Factory-"></a><a name="viewmodelprovider"></a>
<h3 class="api-name" id="ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)">ViewModelProvider</h3>
<pre class="api-signature no-pretty-print">public&nbsp;<a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>&nbsp;owner,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>&nbsp;factory<br>)</pre>
<p>Creates <code>ViewModelProvider</code>, which will create <code>ViewModels</code> via the given <code>Factory</code> and retain them in a store of the given <code>ViewModelStoreOwner</code>.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Parameters</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>&nbsp;owner</code></td>
<td>
<p>a <code>ViewModelStoreOwner</code> whose <code><a href="/reference/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a></code> will be used to retain <code>ViewModels</code></p>
</td>
</tr>
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>&nbsp;factory</code></td>
<td>
<p>a <code>Factory</code> which will be used to instantiate new <code>ViewModels</code></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="api-item"><a name="ViewModelProvider(androidx.lifecycle.ViewModelStore, androidx.lifecycle.ViewModelProvider.Factory, androidx.lifecycle.viewmodel.CreationExtras)"></a><a name="ViewModelProvider-androidx.lifecycle.ViewModelStore-androidx.lifecycle.ViewModelProvider.Factory-androidx.lifecycle.viewmodel.CreationExtras-"></a><a name="viewmodelprovider"></a>
<h3 class="api-name" id="ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)">ViewModelProvider</h3>
<pre class="api-signature no-pretty-print">public&nbsp;<a href="/reference/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a>&nbsp;store,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>&nbsp;factory,<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/viewmodel/CreationExtras.html">CreationExtras</a>&nbsp;defaultCreationExtras<br>)</pre>
</div>
</div>
<div class="list">
<h2>Public methods</h2>
<div class="api-item"><a name="get-java.lang.Class-"></a><a name="get"></a>
<h3 class="api-name" id="get(java.lang.Class)">get</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>public&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&nbsp;&lt;T&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">get</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass)</pre>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
<p>The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Parameters</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass</code></td>
<td>
<p>The class of the ViewModel to create an instance of it if it is not present.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Returns</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T</code></td>
<td>
<p>A ViewModel that is an instance of the given type <code>T</code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Throws</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="https://developer.android.com/reference/java/lang/IllegalArgumentException.html">java.lang.IllegalArgumentException</a>&nbsp;java.lang.IllegalArgumentException</code></td>
<td>
<p>if the given <code><a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">modelClass</a></code> is local or anonymous class.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="api-item"><a name="get(kotlin.String, java.lang.Class)"></a><a name="get-kotlin.String-java.lang.Class-"></a><a name="get"></a>
<h3 class="api-name" id="get(kotlin.String,java.lang.Class)">get</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>public&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&nbsp;&lt;T&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(kotlin.String,java.lang.Class)">get</a>(@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/String.html">String</a>&nbsp;key,&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass)</pre>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
<p>The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed).</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Parameters</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/String.html">String</a>&nbsp;key</code></td>
<td>
<p>The key to use to identify the ViewModel.</p>
</td>
</tr>
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T&gt;&nbsp;modelClass</code></td>
<td>
<p>The class of the ViewModel to create an instance of it if it is not present.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">Returns</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> T</code></td>
<td>
<p>A ViewModel that is an instance of the given type <code>T</code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="list">
<h2>Extension functions</h2>
<div class="api-item"><a name="-androidx.lifecycle.ViewModelProvider-.get--"></a><a name="get"></a>
<h3 class="api-name" id="(androidx.lifecycle.ViewModelProvider).get()">ViewModelProviderGetKt.get</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/androidx/annotation/MainThread.html">MainThread</a><br>public&nbsp;final&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> VM&nbsp;&lt;VM&nbsp;extends&nbsp;<a href="/reference/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/androidx/lifecycle/ViewModelProviderGetKt.html">ViewModelProviderGetKt</a>.<a href="/reference/androidx/lifecycle/ViewModelProvider.html#(androidx.lifecycle.ViewModelProvider).get()">get</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;@<a href="/reference/androidx/annotation/NonNull.html">NonNull</a> <a href="/reference/androidx/lifecycle/ViewModelProvider.html">ViewModelProvider</a>&nbsp;receiver<br>)</pre>
<p>Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this <code>ViewModelProvider</code>.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<colgroup>
<col width="40%">
<col>
</colgroup>
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">get</a></code></td>
<td>
<p>(Class)</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>