Skip to content

Commit

Permalink
Fix appearance changed into wrong activity (#34)
Browse files Browse the repository at this point in the history
the original `AppCompatDelegate.setDefaultNightMode()` is a global setup. when changing the night mode, it will actually call HomeActivity's `onConfigurationChanged`. this pr tries to set night mode to current activity by using the `setLocalNightMode()`
  • Loading branch information
Kudo authored and gabrieldonadel committed Oct 23, 2024
1 parent 5374d93 commit b50a12f
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ constructor(
}

public override fun setColorScheme(style: String) {
when (style) {
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"unspecified" ->
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
var nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
if (style == "dark") {
nightMode = AppCompatDelegate.MODE_NIGHT_YES
} else if (style == "light") {
nightMode = AppCompatDelegate.MODE_NIGHT_NO
} else if (style == "unspecified") {
nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}

val activity = currentActivity
if (activity is AppCompatActivity) {
(activity as AppCompatActivity).delegate.localNightMode = nightMode
} else {
AppCompatDelegate.setDefaultNightMode(nightMode)
}
}

Expand Down

0 comments on commit b50a12f

Please sign in to comment.