Fix: Set "new task flag" for intent method (#29000)

Summary:
Addresses https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Krzysztof Borowy
2021-09-13 12:54:58 -07:00
committed by Facebook GitHub Bot
parent a8cd8f7696
commit 04fe3ed80d
@@ -86,25 +86,8 @@ public class IntentModule extends NativeIntentAndroidSpec {
}
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) {
@@ -235,6 +218,27 @@ public class IntentModule extends NativeIntentAndroidSpec {
}
}
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);
}
}
}