4

I'd like to make status bar hidden and I've managed to do it like so using the accompanist library:

val systemUiController = rememberSystemUiController()
systemUiController.isStatusBarVisible = false

The issue is that when the app goes to background and comes to foreground, this piece of code is not run and therefore the status bar is shown again. How can I fix that?

Thanks.

Arash Shahzamani
  • 245
  • 5
  • 11
  • Hi! Did my answer solve your question? If so, please accept it. Otherwise, let me know if you have any problems with it. – Phil Dukhov Oct 15 '21 at 18:04

1 Answers1

9

You can use OnLifecycleEvent from this answer.

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)
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220