onNextResume
suspend fun <T> LifecycleOwner.onNextResume(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T?
Content copied to clipboard
Executes block
one time, the next time the Lifecycle's state is at least Lifecycle.State.RESUMED.
If the lifecycle is already in this state, block
will be executed immediately.
Samples
import dispatch.android.lifecycle.LifecycleScopeFactory
import dispatch.android.lifecycle.dispatchLifecycleScope
import dispatch.android.lifecycle.onNextCreate
import dispatch.android.lifecycle.onNextResume
import dispatch.android.lifecycle.onNextStart
import dispatch.core.launchMainImmediate
import dispatch.internal.test.android.LiveDataTest
import dispatch.test.CoroutineTest
import dispatch.test.TestProvidedCoroutineScope
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
fun main() {
//sampleStart
runBlocking {
class SomeFragment : Fragment() {
var invocations = 0
init {
dispatchLifecycleScope.launchMainImmediate {
onNextResume { invocations++ }
}
}
}
// current view lifecycle state is INITIALIZED
val fragment = SomeFragment()
// nothing is invoked yet
fragment.invocations shouldBe 0
fragment.resume()
fragment.invocations shouldBe 1
// crossing the threshold doesn't invoke the lambda again
fragment.pause()
fragment.resume()
fragment.invocations shouldBe 1
}
//sampleEnd
}
See also
for repeating behavior.
Parameters
T
the type to be returned by block
context
optional - additional to CoroutineScope.coroutineContext context of the coroutine.
block
the action to be performed
suspend fun <T> Lifecycle.onNextResume(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T?
Content copied to clipboard
Executes block
one time, the next time the Lifecycle's state is at least Lifecycle.State.RESUMED.
If the lifecycle is already in this state, block
will be executed immediately.
Samples
import dispatch.android.lifecycle.LifecycleScopeFactory
import dispatch.android.lifecycle.dispatchLifecycleScope
import dispatch.android.lifecycle.onNextCreate
import dispatch.android.lifecycle.onNextResume
import dispatch.android.lifecycle.onNextStart
import dispatch.core.launchMainImmediate
import dispatch.internal.test.android.LiveDataTest
import dispatch.test.CoroutineTest
import dispatch.test.TestProvidedCoroutineScope
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
fun main() {
//sampleStart
runBlocking {
class SomeFragment : Fragment() {
var invocations = 0
init {
dispatchLifecycleScope.launchMainImmediate {
lifecycle.onNextResume { invocations++ }
}
}
}
// current lifecycle state is INITIALIZED
val fragment = SomeFragment()
// nothing is invoked yet
fragment.invocations shouldBe 0
fragment.resume()
fragment.invocations shouldBe 1
// crossing the threshold doesn't invoke the lambda again
fragment.pause()
fragment.resume()
fragment.invocations shouldBe 1
}
//sampleEnd
}
See also
for repeating behavior.
Parameters
T
the type to be returned by block
context
optional - additional to CoroutineScope.coroutineContext context of the coroutine.
block
the action to be performed