Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any callback for android when ReactContext is created in MainAcitivity? #3887

Closed
yychun1217 opened this issue Nov 4, 2015 · 9 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@yychun1217
Copy link

I checked that it is very strange that ReactInstanceManager.getCurrentReactContext in MainActivity would return null in onCreate and in onResume as well. I found that it should have been created in initializeReactContext() inside ReactInstanceManager, which is called when mReactRootView.initializeReactContext() is invoked.

Yet the context is still null, any hints?

@scgough
Copy link

scgough commented Jan 27, 2016

I'm gettting the same issue when trying to track Android AppState. I've added a SO question here: http://stackoverflow.com/questions/35041962/react-native-android-reactcontext-returning-null

Hope this is solvable!

@scgough
Copy link

scgough commented Jan 27, 2016

Hi @yychun1217 - I have created this solution for returning AppState (i.e. active or background) in Android today. Perhaps you can adapt it for your needs?

https://github.com/scgough/appstateandroid

@satya164
Copy link
Contributor

You get the context in constructor of native modules. So there is really no reason you should need it in onCreate of an activity.

Closing this as tese kinds of questions are more suited towards stackoverflow.

On another note, AppState is in master now - c2d75d7

@pvllnspk
Copy link

pvllnspk commented Aug 5, 2016

I think it's worth to reopen it, any kind of a such callback would be useful

@satya164
Copy link
Contributor

satya164 commented Aug 5, 2016

@pvllnspk What's your use case?

@pvllnspk
Copy link

f.e. send an initial event to js
like 'show me ''Loading' title'

@mehcode
Copy link
Contributor

mehcode commented Aug 19, 2016

@pvllnspk

An initial event? Not sure I understand.

If you're talking about Resume you can get that by listening for the host activity easily enough through a native module or a package like rn-device-activity

If you're talking about really just wanting a way to show a loading screen while JS is loading you probably want a JS-controlled splash screen with something like rn-splash-screen

@lucas-tulio
Copy link

lucas-tulio commented Sep 26, 2017

I had the same problem with ReactContext being null up to a few seconds after my app started. Since I needed to emit an event to JS as soon as possible, I ended up with random crashes on startup due to a null ReactContext object. To avoid the race condition caused by ReactNative starting up, you can listen to the onReactContextInitialized callback.

First, make your Activity/Fragment implement ReactInstanceManager.ReactInstanceEventListener, then add itself as a listener:

@Override
public void onResume() {
    super.onResume();
    getReactInstanceManager().addReactInstanceEventListener(this);
}

@Override
public void onPause() {
    super.onPause();
    getReactInstanceManager().removeReactInstanceEventListener(this);
}

When your ReactContext is created, the callback will be notified:

@Override
public void onReactContextInitialized(ReactContext context) {
    Log.d(TAG, "Here's your valid ReactContext");
}

@mieszko4
Copy link

mieszko4 commented Nov 6, 2017

@lucas-tulio Why hooking to onResume/onPause and not to onCreate/onDestory?

@facebook facebook locked as resolved and limited conversation to collaborators Jul 20, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

8 participants