getCurrentActivity sometimes null inside componentDidMount
See original GitHub issueWe’re seeing a lot of users experience “no activity” errors from react-native-google-signin: https://github.com/devfd/react-native-google-signin/blob/9336cc410ad3b939d0d9637d851311c33dbf3920/android/src/main/java/co/apptailor/googlesignin/RNGoogleSigninModule.java#L82
Our code does:
GoogleSignin.hasPlayServices({ autoResolve: true })
which calls that method.
We do that inside componentDidMount:
private onAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === 'active') {
this.initializeApp(); // <-- called here.
}
}
componentDidMount() {
if (AppState.currentState === 'active') {
this.initializeApp(); // <-- called here.
}
AppState.addEventListener('change', this.onAppStateChange);
}
and that calls into react-native-google-signin:
public void playServicesAvailable(boolean autoresolve, Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.reject("NO_ACTIVITY", "no activity");
return;
}
// ...
}
Originally we just called initializeApp() directly and were experiencing this error. I tried switching to checking the AppState and doing it there just in case, but that still doesn’t appear to be working.
What’s extra confusing about this is that the user sees the loading spinner first, so there’s some activity displayed, and then the error comes back from react-native-google-signin and we show them the error message as well.
So there’s both a rendered activity on the screen and somehow getCurrentActivity returning null at the same time?
Environment
We’re seeing this on Android 23-27.
Environment: OS: macOS High Sierra 10.13.3 Node: 8.9.4 Yarn: 1.5.1 npm: 5.6.0 Watchman: 4.7.0 Xcode: Xcode 9.2 Build version 9C40b Android Studio: 3.0 AI-171.4443003
Packages: (wanted => installed) react: 16.2.0 => 16.2.0 react-native: 0.52.2 => 0.52.2
Expected Behavior
Calling a native method that uses getCurrentActivity from a lifecycle method should always work.
Actual Behavior
It seems to work fine in the android emulator and on all the test devices we have locally, unfortunately we’re getting lots of errors in the wild about it.
Steps to Reproduce
Not sure how… how can getCurrentActivity be null inside componentDidMount?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:28 (10 by maintainers)
@esprehn, I’m having the same problem with a native module we have built for Google Pay with React Native 0.55.3. We can’t reproduce in when testing, but we are seeing crashes in production happening where
getCurrentActivity
is returningnull
@vovkasm I understand how the bridge works, what’s not clear is why react would be mounting components on startup when getCurrentActivity is null.
See ex. https://github.com/facebook/react-native/issues/9310
fwiw there’s an internal team here that has a whole helper class that you pass callbacks into which calls you immediately if getCurrentActivity is non-null, and waits for it to become non-null otherwise. What does Facebook do internally to handle this situation? It seems like calling getCurrentActivity inside a
@ReactMethod
is never safe. Are you supposed to just retry all calls from JS?