how do i notify a particular compose view that the...
# compose
m
how do i notify a particular compose view that the night mode has changed? Details in thread
I have a single activity with FragmentContainerView and a single custom composable view (FrameLayout, addView(ComposeView(...))) I also have all configChanges setted in manifest. Whenever i change dark mode, every fragment updates just right, except my custom view. For some reason, it is not updated. isSystemInDarkTheme() always return the initial value. What i did as a workaround, was to add a listener to my custom view as follows: this.context.findActivity() .addOnConfigurationChangedListener { val darkTheme = when (it.uiMode and Configuration.UI_MODE_NIGHT_MASK) { Configuration.UI_MODE_NIGHT_NO -> false else -> true } if (darkTheme != this.darkTheme.value) { this.darkTheme.value = darkTheme } } and manually set the darkTheme on my custom compose view, disregarding isSystemInDarkTheme(). It behaves alright. But i would like to remove this workaround. Any ideas?
Act Layout:
Copy code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.prozis.main_app.ui.dashboard.view.BottomBarScaffoldView
        android:id="@+id/bottomBarScaffoldView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>
custom compose view:
Copy code
abstract class BridgeComposeFrameLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : FrameLayout(context, attrs, defStyleAttr) {

    private val darkTheme = mutableStateOf(
        when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
            Configuration.UI_MODE_NIGHT_NO -> false
            else -> true
        },
    )
    init {
        this.addView(
            ComposeView(this.context).apply {
                setContent(
                    content = {
                        Screen(darkTheme.value)
                    },
                )
            },
        )
        this.context.findActivity()
            .addOnConfigurationChangedListener {
                val darkTheme =
                    when (it.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
                        Configuration.UI_MODE_NIGHT_NO -> false
                        else -> true
                    }
                if (darkTheme != this.darkTheme.value) {
                    this.darkTheme.value = darkTheme
                }
            }
    }

    @Composable
    abstract fun Screen(darkTheme: Boolean)
}
a
isSystemIsDarkTheme
checks the value of the configuration from
LocalConfiguration
directly, which will return a new value when there’s a new configuration, so I’m surprised that the workaround above works but
isSystemInDarkTheme
doesn’t. Do you have a sample when
isSystemInDarkTheme
isn’t updating?
m
sure. i can work on that. give me a few minutes
apparently, i cannot reproduce this anymore. I removed the workaround and it working. I don’t know if some unrelated stuff fixed this, but it is working not as expected in the latest release. Thanks for you help!
👍 1