Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading