onNextStart
suspend fun <T> LifecycleOwner.onNextStart(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.STARTED.
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 {
onNextStart { invocations++ }
}
}
}
// current view lifecycle state is INITIALIZED
val fragment = SomeFragment()
// nothing is invoked yet
fragment.invocations shouldBe 0
fragment.start()
fragment.invocations shouldBe 1
// crossing the threshold doesn't invoke the lambda again
fragment.stop()
fragment.start()
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.onNextStart(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.STARTED.
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.onNextStart { invocations++ }
}
}
}
// current lifecycle state is INITIALIZED
val fragment = SomeFragment()
// nothing is invoked yet
fragment.invocations shouldBe 0
fragment.start()
fragment.invocations shouldBe 1
// crossing the threshold doesn't invoke the lambda again
fragment.stop()
fragment.start()
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