Skip to content

Commit 233006e

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 c2814c2 commit 233006e

File tree

1 file changed

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

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,20 @@ constructor(
7070
}
7171

7272
public override fun setColorScheme(style: String) {
73-
when (style) {
74-
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
75-
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
76-
"unspecified" ->
77-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
73+
var nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
74+
if (style == "dark") {
75+
nightMode = AppCompatDelegate.MODE_NIGHT_YES
76+
} else if (style == "light") {
77+
nightMode = AppCompatDelegate.MODE_NIGHT_NO
78+
} else if (style == "unspecified") {
79+
nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
80+
}
7881

82+
val activity = currentActivity
83+
if (activity is AppCompatActivity) {
84+
(activity as AppCompatActivity).delegate.localNightMode = nightMode
85+
} else {
86+
AppCompatDelegate.setDefaultNightMode(nightMode)
7987
}
8088
}
8189

0 commit comments

Comments
 (0)