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 vonovak committed Feb 25, 2025
1 parent aa215d3 commit fd7c9f8
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,21 @@ constructor(
}

public override fun setColorScheme(style: String) {
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
}

UiThreadUtil.runOnUiThread {
when (style) {
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"unspecified" ->
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
val activity = currentActivity
if (activity is AppCompatActivity) {
activity.delegate.localNightMode = nightMode
} else {
AppCompatDelegate.setDefaultNightMode(nightMode)
}
}
}
Expand Down

0 comments on commit fd7c9f8

Please sign in to comment.