Skip to content

getCurrentActivity sometimes null inside componentDidMount #18345

@esprehn

Description

@esprehn

We'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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRan CommandsOne of our bots successfully processed a command.StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions