Skip to content

Commit

Permalink
Fix: Set "new task flag" for intent method (#29000)
Browse files Browse the repository at this point in the history
Summary:
Addresses #28934

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - When sending OS intents, always set "FLAG_ACTIVITY_NEW_TASK" flag (required by OS).

Pull Request resolved: #29000

Test Plan:
1. Open RNTester on Android
2. Go to Linking section
3. Try opening "POWER_USAGE_SUMMARY" intent
4. App should open settings, instead of crashing

Reviewed By: cortinico

Differential Revision: D30876645

Pulled By: lunaleaps

fbshipit-source-id: e427bfeadf9fb1ae38bf05bfeafd88e6776d71de
  • Loading branch information
Krzysztof Borowy authored and facebook-github-bot committed Sep 13, 2021
1 parent a8cd8f7 commit 04fe3ed
Showing 1 changed file with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,8 @@ public void openURL(String url, Promise promise) {
}

try {
Activity currentActivity = getCurrentActivity();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url).normalizeScheme());

String selfPackageName = getReactApplicationContext().getPackageName();
ComponentName componentName =
intent.resolveActivity(getReactApplicationContext().getPackageManager());
String otherPackageName = (componentName != null ? componentName.getPackageName() : "");

// If there is no currentActivity or we are launching to a different package we need to set
// the FLAG_ACTIVITY_NEW_TASK flag
if (currentActivity == null || !selfPackageName.equals(otherPackageName)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

if (currentActivity != null) {
currentActivity.startActivity(intent);
} else {
getReactApplicationContext().startActivity(intent);
}
sendOSIntent(intent, false);

promise.resolve(true);
} catch (Exception e) {
Expand Down Expand Up @@ -235,6 +218,27 @@ public void sendIntent(String action, @Nullable ReadableArray extras, Promise pr
}
}

getReactApplicationContext().startActivity(intent);
sendOSIntent(intent, true);
}

private void sendOSIntent(Intent intent, Boolean useNewTaskFlag) {
Activity currentActivity = getCurrentActivity();

String selfPackageName = getReactApplicationContext().getPackageName();
ComponentName componentName =
intent.resolveActivity(getReactApplicationContext().getPackageManager());
String otherPackageName = (componentName != null ? componentName.getPackageName() : "");

// If there is no currentActivity or we are launching to a different package we need to set
// the FLAG_ACTIVITY_NEW_TASK flag
if (useNewTaskFlag || currentActivity == null || !selfPackageName.equals(otherPackageName)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

if (currentActivity != null) {
currentActivity.startActivity(intent);
} else {
getReactApplicationContext().startActivity(intent);
}
}
}

0 comments on commit 04fe3ed

Please sign in to comment.