blob: 197071fb3d9a7960fa321263617b332bf0214071 [file] [log] [blame]
<html devsite="true">
<head>
<title>SaveableStateRegistry</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>SaveableStateRegistry</h1>
<p>
<pre>interface <a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html">SaveableStateRegistry</a></pre>
</p>
<hr>
<p>Allows components to save and restore their state using the saved instance state mechanism.</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>interface <a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry.html">SaveableStateRegistry.Entry</a></code></div>
<p>The registry entry which you get when you use <code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#registerProvider(kotlin.String,kotlin.Function0)">registerProvider</a></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>Public functions</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#canBeSaved(kotlin.Any)">canBeSaved</a>(value:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>)</code></div>
<p>Returns true if the value can be saved using this Registry.</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?</code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#consumeRestored(kotlin.String)">consumeRestored</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>)</code></div>
<p>Returns the restored value for the given key.</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html">Map</a>&lt;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html">List</a>&lt;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt;&gt;</code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#performSave()">performSave</a>()</code></div>
<p>Executes all the registered value providers and combines these values into a map.</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry.html">SaveableStateRegistry.Entry</a></code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#registerProvider(kotlin.String,kotlin.Function0)">registerProvider</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;valueProvider:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>)</code></div>
<p>Registers the value provider.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="list">
<h2>Public functions</h2>
<div class="api-item"><a name="canBeSaved-kotlin.Any-"></a><a name="canbesaved"></a>
<h3 class="api-name" id="canBeSaved(kotlin.Any)">canBeSaved</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#canBeSaved(kotlin.Any)">canBeSaved</a>(value:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>):&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html">Boolean</a></pre>
<p>Returns true if the value can be saved using this Registry. The default implementation will return true if this value can be stored in Bundle.</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>value:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a></code></td>
<td>
<p>The value which we want to save using this Registry</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="api-item"><a name="consumeRestored-kotlin.String-"></a><a name="consumerestored"></a>
<h3 class="api-name" id="consumeRestored(kotlin.String)">consumeRestored</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#consumeRestored(kotlin.String)">consumeRestored</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>):&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?</pre>
<p>Returns the restored value for the given key. Once being restored the value is cleared, so you can't restore the same key twice.</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>Key used to save the value</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="api-item"><a name="performSave--"></a><a name="performsave"></a>
<h3 class="api-name" id="performSave()">performSave</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#performSave()">performSave</a>():&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html">Map</a>&lt;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html">List</a>&lt;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt;&gt;</pre>
<p>Executes all the registered value providers and combines these values into a map. We have a list of values for each key as it is allowed to have multiple providers for the same key.</p>
</div>
<div class="api-item"><a name="registerProvider(kotlin.String, kotlin.Function0)"></a><a name="registerProvider-kotlin.String-kotlin.Function0-"></a><a name="registerprovider"></a>
<h3 class="api-name" id="registerProvider(kotlin.String,kotlin.Function0)">registerProvider</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#registerProvider(kotlin.String,kotlin.Function0)">registerProvider</a>(key:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html">String</a>,&nbsp;valueProvider:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>):&nbsp;<a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry.html">SaveableStateRegistry.Entry</a></pre>
<p>Registers the value provider.</p>
<p>There are could be multiple providers registered for the same <code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#registerProvider(kotlin.String,kotlin.Function0)">key</a></code>. In this case the order in which they were registered matters.</p>
<p>Say we registered two providers for the key. One provides &quot;1&quot;, second provides &quot;2&quot;. <code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#performSave()">performSave</a></code> in this case will have listOf(&quot;1&quot;, &quot;2) as a value for the key in the map. And later, when the registry will be recreated with the previously saved values, the first execution of <code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#consumeRestored(kotlin.String)">consumeRestored</a></code> would consume &quot;1&quot; and the second one &quot;2&quot;.</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>Key to use for storing the value</p>
</td>
</tr>
<tr>
<td width="40%"><code>valueProvider:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a></code></td>
<td>
<p>Provides the current value, to be executed when <code><a href="/reference/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.html#performSave()">performSave</a></code> will be triggered to collect all the registered values</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/kotlin/androidx/compose/runtime/saveable/SaveableStateRegistry.Entry.html">SaveableStateRegistry.Entry</a></code></td>
<td>
<p>the registry entry which you can use to unregister the provider</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>