Skip to content

Commit 1766f24

Browse files
Kudogabrieldonadel
authored andcommitted
Fix appearance changed into wrong activity (#34)
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()`
1 parent 20c10d6 commit 1766f24

File tree

1 file changed

+14
-5
lines changed
  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance

1 file changed

+14
-5
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,21 @@ constructor(
6565
}
6666

6767
public override fun setColorScheme(style: String) {
68+
var nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
69+
if (style == "dark") {
70+
nightMode = AppCompatDelegate.MODE_NIGHT_YES
71+
} else if (style == "light") {
72+
nightMode = AppCompatDelegate.MODE_NIGHT_NO
73+
} else if (style == "unspecified") {
74+
nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
75+
}
76+
6877
UiThreadUtil.runOnUiThread {
69-
when (style) {
70-
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
71-
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
72-
"unspecified" ->
73-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
78+
val activity = currentActivity
79+
if (activity is AppCompatActivity) {
80+
activity.delegate.localNightMode = nightMode
81+
} else {
82+
AppCompatDelegate.setDefaultNightMode(nightMode)
7483
}
7584
}
7685
}

0 commit comments

Comments
 (0)