blob: 8129580662e98b9911bf243b2b0fa8b12cec3b13 [file] [log] [blame]
<html devsite="true">
<head>
<title>LifecycleOwner</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>LifecycleOwner</h1>
<p>
<pre>interface <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></pre>
</p>
<div class="devsite-table-wrapper"><devsite-expandable><span class="expand-control jd-sumtable-subclasses">Known direct subclasses
<div class="showalways" id="subclasses-direct"><a href="/reference/kotlin/androidx/lifecycle/LifecycleRegistryOwner.html">LifecycleRegistryOwner</a>, <a href="/reference/kotlin/androidx/lifecycle/LifecycleService.html">LifecycleService</a>, <a href="/reference/kotlin/androidx/lifecycle/ProcessLifecycleOwner.html">ProcessLifecycleOwner</a>, <a href="/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner.html">TestLifecycleOwner</a></div>
</span>
<div id="subclasses-direct-summary">
<div class="devsite-table-wrapper">
<table class="responsive">
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleRegistryOwner.html">LifecycleRegistryOwner</a></code></td>
<td>
<p><strong>This interface is deprecated.</strong>
<p>Use <code>androidx.appcompat.app.AppCompatActivity</code> which extends <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>, so there are no use cases for this class.</p>
</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleService.html">LifecycleService</a></code></td>
<td>
<p>A Service that is also a <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>.</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/ProcessLifecycleOwner.html">ProcessLifecycleOwner</a></code></td>
<td>
<p>Class that provides lifecycle for the whole application process.</p>
</td>
</tr>
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner.html">TestLifecycleOwner</a></code></td>
<td>
<p>Create a <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code> that allows changing the state via the <code><a href="/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner.html#handleLifecycleEvent(androidx.lifecycle.Lifecycle.Event)">handleLifecycleEvent</a></code> method or <code><a href="/reference/kotlin/androidx/lifecycle/testing/TestLifecycleOwner.html#currentState()">currentState</a></code> property.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</devsite-expandable> </div>
<hr>
<p>A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code></td>
<td></td>
</tr>
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/ViewTreeLifecycleOwner.html">ViewTreeLifecycleOwner</a></code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<h2>Summary</h2>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%"><h3>Public functions</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#getLifecycle()">getLifecycle</a>()</code></div>
<p>Returns the Lifecycle of the provider.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%"><h3>Extension functions</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code>suspend T</code></td>
<td>
<div><code>&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenCreated(kotlin.coroutines.SuspendFunction1)">whenCreated</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T)</code></div>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#CREATED">Lifecycle.State.CREATED</a></code> state.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend T</code></td>
<td>
<div><code>&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenResumed(kotlin.coroutines.SuspendFunction1)">whenResumed</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T)</code></div>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#RESUMED">Lifecycle.State.RESUMED</a></code> state.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend T</code></td>
<td>
<div><code>&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenStarted(kotlin.coroutines.SuspendFunction1)">whenStarted</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T)</code></div>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#STARTED">Lifecycle.State.STARTED</a></code> state.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">repeatOnLifecycle</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;state:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a><br>)</code></div>
<p><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s extension function for <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">Lifecycle.repeatOnLifecycle</a></code> to allow an easier call to the API from LifecycleOwners such as Activities and Fragments.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend&nbsp;inline R</code></td>
<td>
<div><code>&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">withCreated</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R)</code></div>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#CREATED">Lifecycle.State.CREATED</a></code> and resume with the result.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend&nbsp;inline R</code></td>
<td>
<div><code>&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">withResumed</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R)</code></div>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#RESUMED">Lifecycle.State.RESUMED</a></code> and resume with the result.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend&nbsp;inline R</code></td>
<td>
<div><code>&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">withStarted</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R)</code></div>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#STARTED">Lifecycle.State.STARTED</a></code> and resume with the result.</p>
</td>
</tr>
<tr>
<td width="40%"><code>suspend&nbsp;inline R</code></td>
<td>
<div><code>&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">withStateAtLeast</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;state:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R<br>)</code></div>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">state</a></code> and resume with the result.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%"><h3>Extension properties</h3></th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleCoroutineScope.html">LifecycleCoroutineScope</a></code></td>
<td>
<div><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).lifecycleScope()">lifecycleScope</a></code></div>
<p><code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a></code> tied to this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Public functions</h2>
<div><a name="getLifecycle--"></a><a name="getlifecycle"></a>
<h3 class="api-name" id="getLifecycle()">getLifecycle</h3>
<pre class="api-signature no-pretty-print">fun&nbsp;<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#getLifecycle()">getLifecycle</a>():&nbsp;<a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></pre>
<p>Returns the Lifecycle of the provider.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">Returns</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code></td>
<td>
<p>The lifecycle of the provider.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>Extension functions</h2>
<div><a name="-androidx.lifecycle.LifecycleOwner-.whenCreated-kotlin.coroutines.SuspendFunction1-"></a><a name="whencreated"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).whenCreated(kotlin.coroutines.SuspendFunction1)">whenCreated</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;fun&nbsp;&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenCreated(kotlin.coroutines.SuspendFunction1)">whenCreated</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T):&nbsp;T</pre>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#CREATED">Lifecycle.State.CREATED</a></code> state.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">whenStateAtLeast</a></code></td>
<td>
<p>for details</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="-androidx.lifecycle.LifecycleOwner-.whenResumed-kotlin.coroutines.SuspendFunction1-"></a><a name="whenresumed"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).whenResumed(kotlin.coroutines.SuspendFunction1)">whenResumed</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;fun&nbsp;&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenResumed(kotlin.coroutines.SuspendFunction1)">whenResumed</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T):&nbsp;T</pre>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#RESUMED">Lifecycle.State.RESUMED</a></code> state.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">whenStateAtLeast</a></code></td>
<td>
<p>for details</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="-androidx.lifecycle.LifecycleOwner-.whenStarted-kotlin.coroutines.SuspendFunction1-"></a><a name="whenstarted"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).whenStarted(kotlin.coroutines.SuspendFunction1)">whenStarted</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;fun&nbsp;&lt;T&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).whenStarted(kotlin.coroutines.SuspendFunction1)">whenStarted</a>(block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> T):&nbsp;T</pre>
<p>Runs the given block when the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is at least in <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#STARTED">Lifecycle.State.STARTED</a></code> state.</p>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).whenStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">whenStateAtLeast</a></code></td>
<td>
<p>for details</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="(androidx.lifecycle.LifecycleOwner).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State, kotlin.coroutines.SuspendFunction1)"></a><a name="-androidx.lifecycle.LifecycleOwner-.repeatOnLifecycle-androidx.lifecycle.Lifecycle.State-kotlin.coroutines.SuspendFunction1-"></a><a name="repeatonlifecycle"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">repeatOnLifecycle</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;fun&nbsp;<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">repeatOnLifecycle</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;state:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;block:&nbsp;suspend&nbsp;<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a>.() <span style="white-space: nowrap;">-&gt;</span> <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a><br>):&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html">Unit</a></pre>
<p><code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s extension function for <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">Lifecycle.repeatOnLifecycle</a></code> to allow an easier call to the API from LifecycleOwners such as Activities and Fragments.</p>
<pre class="prettyprint">class MyActivity : AppCompatActivity() {<br> override fun onCreate(savedInstanceState: Bundle?) {<br> /* ... */<br> // Runs the block of code in a coroutine when the lifecycle is at least STARTED.<br> // The coroutine will be cancelled when the ON_STOP event happens and will<br> // restart executing if the lifecycle receives the ON_START event again.<br> lifecycleScope.launch {<br> repeatOnLifecycle(Lifecycle.State.STARTED) {<br> uiStateFlow.collect { uiState -&gt;<br> updateUi(uiState)<br> }<br> }<br> }<br> }<br> }</pre>
<div class="devsite-table-wrapper">
<table class="responsive">
<thead>
<tr>
<th colspan="100%">See also</th>
</tr>
</thead>
<tbody class="list">
<tr>
<td width="40%"><code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.Lifecycle).repeatOnLifecycle(androidx.lifecycle.Lifecycle.State,kotlin.coroutines.SuspendFunction1)">repeatOnLifecycle</a></code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
<div><a name="-androidx.lifecycle.LifecycleOwner-.withCreated-kotlin.Function0-"></a><a name="withcreated"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">withCreated</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;inline&nbsp;fun&nbsp;&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">withCreated</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R):&nbsp;R</pre>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#CREATED">Lifecycle.State.CREATED</a></code> and resume with the result. Throws the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException.html">CancellationException</a></code> if the lifecycle has reached <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#DESTROYED">Lifecycle.State.DESTROYED</a></code> by the time of the call or before <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withCreated(kotlin.Function0)">block</a></code> is able to run.</p>
</div>
<div><a name="-androidx.lifecycle.LifecycleOwner-.withResumed-kotlin.Function0-"></a><a name="withresumed"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">withResumed</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;inline&nbsp;fun&nbsp;&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">withResumed</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R):&nbsp;R</pre>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#RESUMED">Lifecycle.State.RESUMED</a></code> and resume with the result. Throws the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException.html">CancellationException</a></code> if the lifecycle has reached <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#DESTROYED">Lifecycle.State.DESTROYED</a></code> by the time of the call or before <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withResumed(kotlin.Function0)">block</a></code> is able to run.</p>
</div>
<div><a name="-androidx.lifecycle.LifecycleOwner-.withStarted-kotlin.Function0-"></a><a name="withstarted"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">withStarted</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;inline&nbsp;fun&nbsp;&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">withStarted</a>(crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R):&nbsp;R</pre>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#STARTED">Lifecycle.State.STARTED</a></code> and resume with the result. Throws the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException.html">CancellationException</a></code> if the lifecycle has reached <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#DESTROYED">Lifecycle.State.DESTROYED</a></code> by the time of the call or before <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStarted(kotlin.Function0)">block</a></code> is able to run.</p>
</div>
<div><a name="(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State, kotlin.Function0)"></a><a name="-androidx.lifecycle.LifecycleOwner-.withStateAtLeast-androidx.lifecycle.Lifecycle.State-kotlin.Function0-"></a><a name="withstateatleast"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">withStateAtLeast</h3>
<pre class="api-signature no-pretty-print">suspend&nbsp;inline&nbsp;fun&nbsp;&lt;R&nbsp;:&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html">Any</a>?&gt; <a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">withStateAtLeast</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;state:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;crossinline&nbsp;block:&nbsp;() <span style="white-space: nowrap;">-&gt;</span> R<br>):&nbsp;R</pre>
<p>Run <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">block</a></code> with this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> in a <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html">Lifecycle.State</a></code> of at least <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">state</a></code> and resume with the result. Throws the <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleDestroyedException.html">CancellationException</a></code> if the lifecycle has reached <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.State.html#DESTROYED">Lifecycle.State.DESTROYED</a></code> by the time of the call or before <code><a href="/reference/kotlin/androidx/lifecycle/package-summary.html#(androidx.lifecycle.LifecycleOwner).withStateAtLeast(androidx.lifecycle.Lifecycle.State,kotlin.Function0)">block</a></code> is able to run.</p>
</div>
<h2>Extension properties</h2>
<div><a name="(androidx.lifecycle.LifecycleOwner).getLifecycleScope()"></a><a name="(androidx.lifecycle.LifecycleOwner).setLifecycleScope()"></a><a name="-androidx.lifecycle.LifecycleOwner-.getLifecycleScope--"></a><a name="-androidx.lifecycle.LifecycleOwner-.setLifecycleScope--"></a>
<h3 class="api-name" id="(androidx.lifecycle.LifecycleOwner).lifecycleScope()">lifecycleScope</h3>
<pre class="api-signature no-pretty-print">val&nbsp;<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a>.<a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html#(androidx.lifecycle.LifecycleOwner).lifecycleScope()">lifecycleScope</a>:&nbsp;<a href="/reference/kotlin/androidx/lifecycle/LifecycleCoroutineScope.html">LifecycleCoroutineScope</a></pre>
<p><code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a></code> tied to this <code><a href="/reference/kotlin/androidx/lifecycle/LifecycleOwner.html">LifecycleOwner</a></code>'s <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code>.</p>
<p>This scope will be cancelled when the <code><a href="/reference/kotlin/androidx/lifecycle/Lifecycle.html">Lifecycle</a></code> is destroyed.</p>
<p>This scope is bound to <code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-main-coroutine-dispatcher/immediate.html">Dispatchers.Main.immediate</a></code>.</p>
</div>
</body>
</html>