blob: 9d8453621bb51c14913a46e59cc1aeb5c9ca9f32 [file] [log] [blame]
<html devsite="true">
<head>
<title>ViewModelProvider</title>
{% setvar book_path %}/reference/kotlin/androidx/_book.yaml{% endsetvar %}
{% include "_shared/_reference-head-tags.html" %}
</head>
<body>
<div id="metadata-info-block"></div>
<h1>ViewModelProvider</h1>
<p>
<pre>open class <a href="/reference/kotlin/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>open class <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory.html">ViewModelProvider.AndroidViewModelFactory</a> : <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.html">ViewModelProvider.NewInstanceFactory</a></code></div>
<p><code><a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">Factory</a></code> which may create <code><a href="/reference/kotlin/androidx/lifecycle/AndroidViewModel.html">AndroidViewModel</a></code> and <code><a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a></code>, which have an empty constructor.</p>
</td>
</tr>
<tr>
<td>
<div><code>object <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.AndroidViewModelFactory.Companion.html">ViewModelProvider.AndroidViewModelFactory.Companion</a></code></div>
</td>
</tr>
<tr>
<td>
<div><code>interface <a href="/reference/kotlin/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>object <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.Companion.html">ViewModelProvider.Factory.Companion</a></code></div>
</td>
</tr>
<tr>
<td>
<div><code>open class <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.NewInstanceFactory.html">ViewModelProvider.NewInstanceFactory</a> : <a href="/reference/kotlin/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>object <a href="/reference/kotlin/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/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)">ViewModelProvider</a>(owner:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>)</code></div>
<p>Creates <code>ViewModelProvider</code>.</p>
</td>
</tr>
<tr>
<td>
<div><code><a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;owner:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;factory:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a><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/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;store:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;factory:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;defaultCreationExtras:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras.html">CreationExtras</a><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 functions</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>open&nbsp;operator T</code></td>
<td>
<div><code>@<a href="/reference/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>&lt;T&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">get</a>(modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;)</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>open&nbsp;operator T</code></td>
<td>
<div><code>@<a href="/reference/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>&lt;T&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#get(kotlin.String,java.lang.Class)">get</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;)</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>inline VM</code></td>
<td>
<div><code>@<a href="/reference/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>&lt;VM&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html">ViewModelProvider</a>.<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#(androidx.lifecycle.ViewModelProvider).get()">get</a>()</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"><a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner)">ViewModelProvider</a>(owner:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>)</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/kotlin/androidx/lifecycle/HasDefaultViewModelProviderFactory.html#getDefaultViewModelProviderFactory()">default factory</a></code> if the owner implements <code><a href="/reference/kotlin/androidx/lifecycle/HasDefaultViewModelProviderFactory.html">HasDefaultViewModelProviderFactory</a></code>. Otherwise, a <code><a href="/reference/kotlin/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"><a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStoreOwner,androidx.lifecycle.ViewModelProvider.Factory)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;owner:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;factory:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a><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>owner:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner.html">ViewModelStoreOwner</a></code></td>
<td>
<p>a <code>ViewModelStoreOwner</code> whose <code><a href="/reference/kotlin/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a></code> will be used to retain <code>ViewModels</code></p>
</td>
</tr>
<tr>
<td width="40%"><code>factory:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a></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"><a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#ViewModelProvider(androidx.lifecycle.ViewModelStore,androidx.lifecycle.ViewModelProvider.Factory,androidx.lifecycle.viewmodel.CreationExtras)">ViewModelProvider</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;store:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelStore.html">ViewModelStore</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;factory:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.Factory.html">ViewModelProvider.Factory</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;defaultCreationExtras:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/viewmodel/CreationExtras.html">CreationExtras</a> = CreationExtras.Empty<br>)</pre>
</div>
</div>
<div class="list">
<h2>Public functions</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/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>open&nbsp;operator&nbsp;fun&nbsp;&lt;T&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#get(java.lang.Class)">get</a>(modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;):&nbsp;T</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>modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;</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>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>java.lang.IllegalArgumentException:&nbsp;<a href="https://developer.android.com/reference/java/lang/IllegalArgumentException.html">java.lang.IllegalArgumentException</a></code></td>
<td>
<p>if the given <code><a href="/reference/kotlin/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/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>open&nbsp;operator&nbsp;fun&nbsp;&lt;T&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#get(kotlin.String,java.lang.Class)">get</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;):&nbsp;T</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>key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a></code></td>
<td>
<p>The key to use to identify the ViewModel.</p>
</td>
</tr>
<tr>
<td width="40%"><code>modelClass:&nbsp;<a href="https://developer.android.com/reference/java/lang/Class.html">Class</a>&lt;T&gt;</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>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()">get</h3>
<pre class="api-signature no-pretty-print">@<a href="/reference/kotlin/androidx/annotation/MainThread.html">MainThread</a><br>inline&nbsp;fun&nbsp;&lt;VM&nbsp;:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/ViewModel.html">ViewModel</a>&gt; <a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html">ViewModelProvider</a>.<a href="/reference/kotlin/androidx/lifecycle/ViewModelProvider.html#(androidx.lifecycle.ViewModelProvider).get()">get</a>():&nbsp;VM</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/kotlin/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>