Changing a device theme does not effect my Jetpack Compose theme. Anyone faced this issue before?
K Merle
05/02/2024, 10:01 AM
I've found a reason. Seems like using
Copy code
MainActivity : AppCompatActivity()
breaks theme change while this doesnt
Copy code
MainActivity : ComponentActivity()
a
Alex Styl
05/02/2024, 10:26 AM
what is a 'device theme'?
k
K Merle
05/02/2024, 10:27 AM
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
)
}
K Merle
05/02/2024, 10:28 AM
I do have to use
AppCompatActivity
cause of localization library we're using called Phrase.
a
Alex Styl
05/02/2024, 10:31 AM
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
youssef
05/08/2024, 4:51 PM
check in the theme if you provide remember for colorsheme
like so