diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 8410dcefed9..b3079e3ac7b 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -30,6 +30,8 @@ using namespace facebook::react; BOOL _isFullscreen; std::atomic _invalidated; NSDictionary *_constants; + + __weak UIWindow *_applicationWindow; } static NSString *const kFrameKeyPath = @"frame"; @@ -41,7 +43,8 @@ RCT_EXPORT_MODULE() - (instancetype)init { if (self = [super init]) { - [RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; + _applicationWindow = RCTKeyWindow(); + [_applicationWindow addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; } return self; } @@ -141,7 +144,7 @@ RCT_EXPORT_MODULE() [[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil]; - [RCTKeyWindow() removeObserver:self forKeyPath:kFrameKeyPath]; + [_applicationWindow removeObserver:self forKeyPath:kFrameKeyPath]; #if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];