Android 处理 Jetpack Compose 屏幕上的生命周期事件

Abu*_*suf 8 android android-lifecycle android-jetpack android-jetpack-compose

在 Jetpack Compose 中,所有屏幕都是可组合功能。FragmentsJetpack Compose 中未使用。

我们如何使用 Jetpack Compose 处理生命周期事件?如果我们使用 Fragment,我们可以处理生命周期事件 ( onStart/onPause/onResume/onStop)。这些对于释放资源、停止观察更改等不同场景很有用。

在 Jetpack Compose 中,如果我需要处理这些事件,我该如何在可组合屏幕中处理这些事件?

请帮助我提供一些想法或资源,以便我能够理解这一点。

提前致谢。

Thr*_*ian 18

您可以获得 LifecycleOwner val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current

LifecycleEventObserver使用DisposableEffectonDispose 函数注册并删除该观察者DisposableEffect

如果您传递的函数在重组过程中可能会发生变化,您也可以使用 RememberUpdatedState。

我添加一个样本

@Composable
private fun DisposableEffectWithLifeCycle(
    onResume: () -> Unit,
    onPause: () -> Unit,
) {

    val context = LocalContext.current

    // Safely update the current lambdas when a new one is provided
    val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current

    Toast.makeText(
        context,
        "DisposableEffectWithLifeCycle composition ENTER",
        Toast.LENGTH_SHORT
    ).show()

    val currentOnResume by rememberUpdatedState(onResume)
    val currentOnPause by rememberUpdatedState(onPause)

    // If `lifecycleOwner` changes, dispose and reset the effect
    DisposableEffect(lifecycleOwner) {
        // Create an observer that triggers our remembered callbacks
        // for lifecycle events
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_CREATE -> {
                    Toast.makeText(
                        context,
                        "DisposableEffectWithLifeCycle ON_CREATE",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                Lifecycle.Event.ON_START -> {
                    Toast.makeText(
                        context,
                        "DisposableEffectWithLifeCycle ON_START",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                Lifecycle.Event.ON_RESUME -> {
                    currentOnResume()
                }
                Lifecycle.Event.ON_PAUSE -> {
                    currentOnPause()
                }
                Lifecycle.Event.ON_STOP -> {
                    Toast.makeText(
                        context,
                        "DisposableEffectWithLifeCycle ON_STOP",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                Lifecycle.Event.ON_DESTROY -> {
                    Toast.makeText(
                        context,
                        "DisposableEffectWithLifeCycle ON_DESTROY",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                else -> {}
            }
        }

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

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

            Toast.makeText(
                context,
                "DisposableEffectWithLifeCycle composition EXIT",
                Toast.LENGTH_SHORT
            )
                .show()
        }
    }

    Column(modifier = Modifier.background(Color(0xff03A9F4))) {
        Text(
            text = "Disposable Effect with lifecycle",
            color = Color.White,
            modifier = Modifier
                .padding(8.dp)
                .fillMaxWidth()
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

示范

val context = LocalContext.current

DisposableEffectWithLifeCycle(
    onResume = {
        Toast.makeText(
            context,
            "DisposableEffectWithLifeCycle onResume()",
            Toast.LENGTH_SHORT
        )
            .show()
    },
    onPause = {
        Toast.makeText(
            context,
            "DisposableEffectWithLifeCycle onPause()",
            Toast.LENGTH_SHORT
        )
            .show()
    }

)
Run Code Online (Sandbox Code Playgroud)