gpt4 book ai didi

android-jetpack-compose - Jetpack compose 来到前台时如何重新组合

转载 作者:行者123 更新时间:2023-12-02 18:31:28 27 4
gpt4 key购买 nike

我想隐藏状态栏,我已经使用伴奏库做到了这一点:

val systemUiController = rememberSystemUiController()
systemUiController.isStatusBarVisible = false

问题是,当应用程序进入后台并进入前台时,这段代码不会运行,因此会再次显示状态栏。我该如何解决?

谢谢。

最佳答案

您可以使用 this answer 中的 OnLifecycleEvent .

val systemUiController = rememberSystemUiController()
OnLifecycleEvent { _, event ->
when (event) {
Lifecycle.Event.ON_RESUME,
Lifecycle.Event.ON_START,
-> {
systemUiController.isStatusBarVisible = false
}
else -> Unit
}
}

OnLifecycleEvent:

@Composable
fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) {
val eventHandler = rememberUpdatedState(onEvent)
val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current)

DisposableEffect(lifecycleOwner.value) {
val lifecycle = lifecycleOwner.value.lifecycle
val observer = LifecycleEventObserver { owner, event ->
eventHandler.value(owner, event)
}

lifecycle.addObserver(observer)
onDispose {
lifecycle.removeObserver(observer)
}
}
}

关于android-jetpack-compose - Jetpack compose 来到前台时如何重新组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69387652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com