Android Jetpack Compose rememberSystemUiController() doesn't observe mutableStateOf()

I’m trying to achieve a hide/show functionality for status bar in an AbstractComposeView

class MyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AbstractComposeView(context, attrs, defStyleAttr) {
    private val hide = mutableStateOf(false)
    var hideBars: Boolean
        get() = hide.value
        set(value) { hide.value = value }
    @Composable
    override fun Content() {
        val systemUiController = rememberSystemUiController()
        SideEffect {
            systemUiController.isStatusBarVisible = !hide.value
        }
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindow)
        Launch()
    }
}

When setting hideBars = true/false it’s not having any effect

view.findViewById<MyView>(R.id.my_view).hideBars = true

Any suggestions or workarounds?

>Solution :

SideEffect is just that – a side effect of something else causing a recomposition – changes to values read inside SideEffect do not cause recompositions themselves.

Instead, you should consider using LaunchedEffect, which runs every time the key passed to it changes:

LaunchedEffect(hide.value) {
    systemUiController.isStatusBarVisible = !hide.value
}

Leave a Reply