[英]Android handle lifecycle event on Jetpack Compose Screen
In Jetpack Compose all screen are composable function. Fragments
are not used in Jetpack Compose.在 Jetpack Compose 中,所有屏幕都是可组合的 function。Jetpack Compose 中不使用Fragments
。
How we can handle lifecycle events with Jetpack Compose?我们如何使用 Jetpack Compose 处理生命周期事件? If we use Fragment we can handle lifecycle event ( onStart/onPause/onResume/onStop
).如果我们使用 Fragment,我们可以处理生命周期事件( onStart/onPause/onResume/onStop
)。 Those are useful for difference scenario like releasing resource, stop observing changes etc.这些对于释放资源、停止观察变化等不同场景很有用。
In Jetpack Compose if i need to handle those event how can i do that in Composable Screen?在 Jetpack Compose 中,如果我需要处理这些事件,我该如何在 Composable Screen 中处理这些事件?
Please help me with some idea or resource so that i can understand this.请帮助我提供一些想法或资源,以便我能够理解这一点。
Thanks in advance.提前致谢。
You can get LifecycleOwner val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current
你可以得到 LifecycleOwner val lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current
and register LifecycleEventObserver
using DisposableEffect
and remove this observer on onDispose function of DisposableEffect
.并使用DisposableEffect
注册LifecycleEventObserver
并在DisposableEffect
的 onDispose function 上删除此观察者。
You can also use rememberUpdatedState if the functions you pass might change during recompositions.如果您传递的函数在重组过程中可能会发生变化,您也可以使用 rememberUpdatedState。
I add a sample我添加一个示例
@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()
)
}
}
Demonstration示范
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()
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.