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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def safeExtGet(prop, fallback) {
}

android {
compileSdkVersion safeExtGet('compileSdkVersion', 23)
buildToolsVersion safeExtGet('buildToolsVersion', '23.0.1')
compileSdkVersion safeExtGet('compileSdkVersion', 26)
buildToolsVersion safeExtGet('buildToolsVersion', '26.0.2')

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private String appIdFromManifest(ReactApplicationContext context) {
// However it seems it is also to soon to call getCurrentActivity() from the reactContext as well.
// This will normally succeed when onHostResume fires instead.
private void initOneSignal() {

// Uncomment to debug init issues.
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.ERROR);

Expand Down Expand Up @@ -107,8 +106,23 @@ private JSONObject jsonFromErrorMessageString(String errorMessage) throws JSONEx
@ReactMethod
public void init(String appId) {
Activity activity = getCurrentActivity();
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.

// in some cases, especially with react-native-navigation, it can take a while for the Activity to be created
// if null, we should re-attempt initialization after 50 milliseconds.
final String currentAppId = appId;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
init(currentAppId);
}
}, 50);
return;
}

if (oneSignalInitDone) {
Log.w("onesignal", "The OneSignal SDK has already been initialized");
return;
}

Expand Down