question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getCurrentActivity sometimes null inside componentDidMount

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:28 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
wildseansycommented, May 14, 2018

@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 returning null

3reactions
esprehncommented, Mar 15, 2018

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

context in componentDidMount appears as null - Stack Overflow
Your design here seems flawed i.e. when your provider is mounted you send the API request and then when your descendant component is...
Read more >
Native Modules · React Native
Sometimes an app needs access to a platform API that React Native doesn't have a corresponding module for yet. Maybe you want to...
Read more >
Ref returns undefined or null in React [Solved] | bobbyhadz
A React `ref` most commonly returns `undefined` or `null` when we try to access its `current` property before its corresponding DOM element is...
Read more >
useEffect(fn, []) is not the new componentDidMount()
Component renders for the first time. The return value of render() is used to mount new DOM. componentDidMount fires and sets state immediately ......
Read more >
getCurrentActivity in ReactContextBaseJavaModule returns ...
[Solved]-getCurrentActivity in ReactContextBaseJavaModule returns null-React Native. Search. score:1. public class ExampleModule extends ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found