I am working on an Android project where I need to support switching between Dark/Light themes based on system setting preference.
I have changed App Theme from Theme.AppCompat.Light.NoActionBar
to Theme.AppCompat.DayNight.NoActionBar
.
I am using this API on Application onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
App theme is getting updated only when I restart app but does not update when I am within the app when theme is changed from system setting.
Am I missing any config changes?
I believe that the issue lies with the
android:configChanges
tag. This is because the activity needs to be recreated when the theme is changed. You will have to remove the tag and useonSavedInstanceState
to save the state of your current activity.