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

Fix React Native Null Activity #572

Merged
merged 4 commits into from Jun 22, 2018
Merged

Conversation

Nightsd01
Copy link
Contributor

@Nightsd01 Nightsd01 commented Jun 21, 2018

• In some cases, especially with projects using the react-native-navigation dependency, the initial Activity would be null, preventing the SDK from initializing.
• Added a check that will launch a re-attempt timer if this occurs.
• Updated build.gradle to use a newer version (26.0.2)
Fixes #516, #547

• In some cases, especially with projects using the react-native-navigation dependency, the initial Activity would be null, preventing the SDK from initializing.
• Added a check that will launch a re-attempt timer if this occurs.
• Updated build.gradle to use a newer version (26.0.2)
if (activity == null || oneSignalInitDone) {
Log.e("onesignal", "Unable to initialize the OneSignal SDK because activity is null " + (activity == null) + " or oneSignalInitDone" + oneSignalInitDone);

if (activity == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what values are on mReactApplicationContext but can we get an Android Context from this if getCurrentActivity() gives us null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkasten2 getCurrentActivity() is what is now used in RN to get the current activity. The old way of getting the activity (from the ReactApplicationContext object) is deprecated

https://github.com/facebook/react-native/releases/tag/v0.29.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe mReactApplicationContext.getApplicationContext() should always be non-null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkasten2 mReactApplicationContext.getApplicationContext() behaves the same way.

It seems that as of 2016, React-Native no longer guarantees this method will return a non null activity at the start of the app. So I think I'll just change it to get the activity from mReactApplicationContext and still check for null, and if it's null, re-check in 50ms. It appears this resolves a pretty common issue in RN applications.

• Changes how the SDK gets a reference to the current activity during initialization
• Changed the SDK so that if, during initialization, the initial activity does not exist, the SDK will use the application context instead
• Cleans up initialization of the SDK, since Activity is a subclass of Context, we can simplify the initialization method.
@Nightsd01 Nightsd01 merged commit 3772950 into master Jun 22, 2018
@Nightsd01 Nightsd01 deleted the fix_react_native_navigation branch June 22, 2018 18:14
@avishayil
Copy link
Contributor

Actually I still encounter this. Not seeing my device on the users list and when using function to get the player id it returns null.

@rgomezp
Copy link
Contributor

rgomezp commented Jul 15, 2019

@avishayil
So this was never resolved? Came up recently in #797

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants