Changing a device theme does not effect my Jetpack...
# compose
k
Changing a device theme does not effect my Jetpack Compose theme. Anyone faced this issue before?
I've found a reason. Seems like using
Copy code
MainActivity : AppCompatActivity()
breaks theme change while this doesnt
Copy code
MainActivity : ComponentActivity()
a
what is a 'device theme'?
k
Device or system theme.
AppCompatActivity
doesn't change theme on configuration change, i.e. if you started of with
light
theme, changing in setting does not reflect change in app. Using
ComponentActivity
does work. Seems like explicitly setting it up does the job:
Copy code
private fun setSystemTheme() {
    val uiModeManager = getSystemService(AppCompatActivity.UI_MODE_SERVICE) as UiModeManager
    val isDarkTheme = uiModeManager.nightMode == UiModeManager.MODE_NIGHT_YES
    AppCompatDelegate.setDefaultNightMode(
        if (isDarkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
    )
}
I do have to use
AppCompatActivity
cause of localization library we're using called Phrase.
a
compose on its own doesn't know about android or know what device themes are. this means you would need to trigger a recomposition when the theme changes. AFAIK
ComponentActivity()
is what the Android team recommends using with compose, so i would either open an issue at the library to change their code, or check the source of how ComponentActivity handles this. You will get better help at #compose-android
👍 1
y
check in the theme if you provide remember for colorsheme like so
Copy code
CompositionLocalProvider(
    LocalAppDimens provides dims,
    LocalOrientationMode provides orient,
    LocalQrColorScheme provides remember {colorScheme} ,
while changing the theme it wont work