Very dummy question… Why `isSystemInDarkTheme()` is always returning true even when I switch to ligh...
n
Very dummy question… Why
isSystemInDarkTheme()
is always returning true even when I switch to light mode? 🤔
❤️ 1
More info: here’s my activity code:
Copy code
setContent {
    MyAppTheme {
        ProvideWindowInsets {
            Surface(color = AppTheme.colors.background) {
                AppNavHost(
                    initialRoute = initRoute.route,
                    sharedViewModel = sharedViewModel,
                    onFinishRequest = { finish() },
                    hasAccounts = hasAccount
                )
            }
        }
    }
}
and my activity is using a theme extending from
Theme.MaterialComponents.DayNight.DarkActionBar
i
how do you switch to light mode?
n
via status bar panel option.
a
Can you show us
MyAppTheme
? There's nothing particularly of note in the snippet you posted.
i
hmm, sounds strange.
isSystemInDarkTheme()
should returns
true
when dark theme enabled android-system-wide and
false
if doesn’t. Maybe it is device specific bug?
n
Copy code
@Composable
fun MyAppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    Log.d("NGVL", "darkTheme: $darkTheme ----- ${isSystemInDarkTheme()}")
@Andrew Neal this log is printing… true 😕
@iamthevoid i’m testing on an emulator, but another app is working… not sure what I did wrong…
i
this log printing
Copy code
darkTheme: true ----- true
or
Copy code
darkTheme: true ----- false
? I am not sure that
darkTheme
param changing same way
n
both are true 😕
a
Sounds like less of a Compose thing and more of a general Android thing, just based on your snippets. What's your emulator look like? What version of Android are you running, etc?
isSystemInDarkThem
is just checking the
Configuration
flags.
@Composable
@ReadOnlyComposable
internal actual fun _*isSystemInDarkTheme*(): Boolean {
val uiMode = LocalConfiguration.current.uiMode
return (uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
}
n
sorry… both are false
actually, it gets the initial value and are not updated
@Andrew Neal is API 30 and the device theme is changing. and as I mentioned, other app (using compose) is working fine 😕
I just trying to figure it out what I did wrong on this app
a
Hmm. Yeah, hard to say. You may try testing it on a different emulator. But as far as calling
isSystemInDarkTheme
like you are goes, that looks good.
i
Just for confidence: try to clean-build, sometimes studio glitches)
l
Different version of AppCompat?
n
i’m using app compat
1.4.0-alpha01
l
in your manifest have you added this to your activity:
android:configChanges="uiMode"
? Without it, the theme won't be updated at runtime when you switch darkMode from the statusbar panel
n
I guess it’s the opposite… adding that you’re saying the activity should not be recreated when the uiMode changes… 😕
👍 1
a
@Louis Pullen-Freilich [G]
l
@nglauber if you go for a single activity / compose app you can add everything into configChanges: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1618760507089400?thread_ts=1618691113.058800&cid=CJLTWPH7S
n
@Lucien Guimaraes, thanks but didn’t work for me… I’m not using it in the other app which is working…
I must be doing something very wrong… 😩 I put this in my Activity
Copy code
setContent {
    Log.d("NGVL", "isSystemInDarkTheme = ${isSystemInDarkTheme()}")
When I toggle dark/light mode, it is printed, but just the original value is displayed. I mean, if I open the app in dark mode, it’s always show true, otherwise always show false. 😞
Ok. I found the problem, but now I have another one 😅 In my app class, I’m overriding the
attachBaseContext
to configure the application language. So the context returned by
createConfigurationContext
is not working…
Copy code
val locale = Locale(language)
Locale.setDefault(locale)
val configuration = Configuration(context.resources.configuration)
configuration.setLocale(locale)
return context.createConfigurationContext(configuration)
Suggestions?
t
Huh, I thought
LocaleList.getDefault
would return the correct value
n
Ok. New update… 😄 I was overriding both
onAttachBaseContext
on both app and activity. When I left just in the activity, it’s working… I’ll test language change to check if it’s set.
t
I hooked up
cashapp/copper
to update a MutableState whenever the locale changes, but that's probably unnecessary if Compose can pull it from the Configuration