Skip to content

Commit cf255ca

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 651b025 commit cf255ca

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
@@ -71,12 +71,21 @@ constructor(
7171
}
7272

7373
public override fun setColorScheme(style: String) {
74+
var nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
75+
if (style == "dark") {
76+
nightMode = AppCompatDelegate.MODE_NIGHT_YES
77+
} else if (style == "light") {
78+
nightMode = AppCompatDelegate.MODE_NIGHT_NO
79+
} else if (style == "unspecified") {
80+
nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
81+
}
82+
7483
UiThreadUtil.runOnUiThread {
75-
when (style) {
76-
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
77-
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
78-
"unspecified" ->
79-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
84+
val activity = currentActivity
85+
if (activity is AppCompatActivity) {
86+
activity.delegate.localNightMode = nightMode
87+
} else {
88+
AppCompatDelegate.setDefaultNightMode(nightMode)
8089
}
8190
}
8291
}

0 commit comments

Comments
 (0)