diff --git a/packages/react-native/React/Base/RCTRootView.m b/packages/react-native/React/Base/RCTRootView.m index c7c142075bbc6a..5acd5f03c4c951 100644 --- a/packages/react-native/React/Base/RCTRootView.m +++ b/packages/react-native/React/Base/RCTRootView.m @@ -368,6 +368,9 @@ - (void)contentViewInvalidated - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { [super traitCollectionDidChange:previousTraitCollection]; + if (RCTSharedApplication().applicationState == UIApplicationStateBackground) { + return; + } [[NSNotificationCenter defaultCenter] postNotificationName:RCTUserInterfaceStyleDidChangeNotification diff --git a/packages/react-native/React/CoreModules/RCTAppearance.h b/packages/react-native/React/CoreModules/RCTAppearance.h index caa842d72f6a1b..9909ca9e741d7d 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.h +++ b/packages/react-native/React/CoreModules/RCTAppearance.h @@ -17,4 +17,5 @@ RCT_EXTERN NSString *RCTCurrentOverrideAppearancePreference(); RCT_EXTERN NSString *RCTColorSchemePreference(UITraitCollection *traitCollection); @interface RCTAppearance : RCTEventEmitter +- (instancetype)init; @end diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 2930401fc98f6e..449ff0396be889 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -57,11 +57,7 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride) return RCTAppearanceColorSchemeLight; } - traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection]; return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight; - - // Default to light on older OS version - same behavior as Android. - return RCTAppearanceColorSchemeLight; } @interface RCTAppearance () @@ -71,6 +67,19 @@ @implementation RCTAppearance { NSString *_currentColorScheme; } +- (instancetype)init +{ + if ((self = [super init])) { + UITraitCollection *traitCollection = RCTSharedApplication().delegate.window.traitCollection; + _currentColorScheme = RCTColorSchemePreference(traitCollection); + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(appearanceChanged:) + name:RCTUserInterfaceStyleDidChangeNotification + object:nil]; + } + return self; +} + RCT_EXPORT_MODULE(Appearance) + (BOOL)requiresMainQueueSetup @@ -100,9 +109,6 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) { - if (_currentColorScheme == nil) { - _currentColorScheme = RCTColorSchemePreference(nil); - } return _currentColorScheme; } @@ -129,14 +135,15 @@ - (void)appearanceChanged:(NSNotification *)notification - (void)startObserving { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(appearanceChanged:) - name:RCTUserInterfaceStyleDidChangeNotification - object:nil]; } - (void)stopObserving { +} + +- (void)invalidate +{ + [super invalidate]; [[NSNotificationCenter defaultCenter] removeObserver:self]; }