Jetpackはライフサイクルを構成する


構成可能な関数はXMLファイルの置換です.
古典的な方法では、多くの活動やフラグメントライフサイクルを処理するために使用します.
だから、我々は自分のライフサイクルを持つAndroidの多くのコンポーネントがあるので、ビューライフサイクルに焦点を当てる
> The lifecycle of a composable is defined by the following events: entering the Composition, getting recomposed 0 or more times, and leaving the Composition.

そこでここでは、
したがって、構成可能な関数には3つのライフサイクルがあります.
 -  enter composition
 -  recomposition n times
 -   leave composition
あなたの構成可能な機能のライフサイクルを観察するために、あなたは
> Recomposition is typically triggered by a change to a State object. Compose tracks these and runs all composables in the Composition that read that particular State, and any composables that they call that cannot be skipped.
また、ライフサイクルを理解するためには、副作用を知る必要がある
> A side-effect is a change to the state of the app that happens outside the scope of a composable function.
> An effect is a composable function that doesn't emit UI and causes side effects to run when a composition completes.
合成には多くの副作用関数がある
1).
LaunchedEffect

run suspend functions in the scope of a composable
`


作曲可能な
楽しいmyscreen
状態: Uistate >
scaffoldstate : scaffoldstate = remember scaffddstate ()
){ { } { }
// If the UI state contains an error, show snackbar
if (state.hasError) {

    // `LaunchedEffect` will cancel and re-launch if
    // `scaffoldState.snackbarHostState` changes
    LaunchedEffect(scaffoldState.snackbarHostState) {
        // Show snackbar using a coroutine, when the coroutine is cancelled the
        // snackbar will automatically dismiss. This coroutine will cancel whenever
        // `state.hasError` is false, and only start when `state.hasError` is true
        // (due to the above if-check), or if `scaffoldState.snackbarHostState` changes.
        scaffoldState.snackbarHostState.showSnackbar(
            message = "Error message",
            actionLabel = "Retry message"
        )
    }
}

Scaffold(scaffoldState = scaffoldState) {
    /* ... */
}

2).
rememberCoroutineScope

obtain a composition-aware scope to launch a coroutine `outside a composable


作曲可能な
楽しいMovieScreen ( scaffoldstate : scaffoldstate = remember - scafftate () ){ { }
// Creates a CoroutineScope bound to the MoviesScreen's lifecycle
val scope = rememberCoroutineScope()

Scaffold(scaffoldState = scaffoldState) {
    Column {
        /* ... */
        Button(
            onClick = {
                // Create a new coroutine in the event handler to show a snackbar
                scope.launch {
                    scaffoldState.snackbarHostState.showSnackbar("Something happened!")
                }
            }
        ) {
            Text("Press me")
        }
    }
}

3).
rememberUpdatedState
<>参照値を変更した場合に再起動しない効果の値
作曲可能な
楽しいlandingscreen ( ontimeout::(-)>ユニット{ }
// This will always refer to the latest onTimeout function that
// LandingScreen was recomposed with
val currentOnTimeout by rememberUpdatedState(onTimeout)

// Create an effect that matches the lifecycle of LandingScreen.
// If LandingScreen recomposes, the delay shouldn't start again.
LaunchedEffect(true) {
    delay(SplashWaitTimeMillis)
    currentOnTimeout()
}

/* Landing screen content */

`
4).
DisposableEffect
クリーンアップを必要とする効果
合成可能な関数が構成を残すとき
`
作曲可能な
楽しいhomescreen
LifeCleowner : lifecycleowner = localfeycleowner .カレント
onStart :(-)> unit ,\/開始した解析イベントを送信する
onstop :()> unit/send ' stop '解析イベントを送信する
){ { } { }
//現在のラムダを安全に更新する
RembertUpdatedStatusによるval currentOnStart ( onstart )
RememberupDateDateによるval currentOnstop ( Onstop )
// If `lifecycleOwner` changes, dispose and reset the effect
DisposableEffect(lifecycleOwner) {
    // Create an observer that triggers our remembered callbacks
    // for sending analytics events
    val observer = LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_START) {
            currentOnStart()
        } else if (event == Lifecycle.Event.ON_STOP) {
            currentOnStop()
        }
    }

    // Add the observer to the lifecycle
    lifecycleOwner.lifecycle.addObserver(observer)

    // When the effect leaves the Composition, remove the observer
    onDispose {
        lifecycleOwner.lifecycle.removeObserver(observer)
    }
}

/* Home screen content */

`
楽しんでください