onNextCreate

suspend fun <T> LifecycleOwner.onNextCreate(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T?

Executes block one time, the next time the Lifecycle's state is at least Lifecycle.State.CREATED.

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 {
          onNextCreate { invocations++ }
        }
      }
    }

    // current view lifecycle state is INITIALIZED
    val fragment = SomeFragment()

    // nothing is invoked yet
    fragment.invocations shouldBe 0

    fragment.create()
    fragment.invocations shouldBe 1
  } 
   //sampleEnd
}

See also

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.onNextCreate(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T?

Executes block one time, the next time the Lifecycle's state is at least Lifecycle.State.CREATED.

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.onNextCreate { invocations++ }
        }
      }
    }

    // current lifecycle state is INITIALIZED
    val fragment = SomeFragment()

    // nothing is invoked yet
    fragment.invocations shouldBe 0

    fragment.create()
    fragment.invocations shouldBe 1
  } 
   //sampleEnd
}

See also

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