Skip to content

Commit b69e91c

Browse files
Kudoalanjhughes
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 54d30ec commit b69e91c

File tree

1 file changed

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

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.modules.appearance
99

1010
import android.content.Context
11+
import androidx.appcompat.app.AppCompatActivity
1112
import androidx.appcompat.app.AppCompatDelegate
1213
import com.facebook.fbreact.specs.NativeAppearanceSpec
1314
import com.facebook.react.bridge.ReactApplicationContext
@@ -53,12 +54,21 @@ constructor(
5354
}
5455

5556
public override fun setColorScheme(style: String) {
57+
var nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
58+
if (style == "dark") {
59+
nightMode = AppCompatDelegate.MODE_NIGHT_YES
60+
} else if (style == "light") {
61+
nightMode = AppCompatDelegate.MODE_NIGHT_NO
62+
} else if (style == "unspecified") {
63+
nightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
64+
}
65+
5666
UiThreadUtil.runOnUiThread {
57-
when (style) {
58-
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
59-
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
60-
"unspecified" ->
61-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
67+
val activity = currentActivity
68+
if (activity is AppCompatActivity) {
69+
activity.delegate.localNightMode = nightMode
70+
} else {
71+
AppCompatDelegate.setDefaultNightMode(nightMode)
6272
}
6373
}
6474
}

0 commit comments

Comments
 (0)