From 04fe3ed80d9c201a483a2b477aeebd3d4169fd6d Mon Sep 17 00:00:00 2001 From: Krzysztof Borowy Date: Mon, 13 Sep 2021 12:54:58 -0700 Subject: [PATCH] Fix: Set "new task flag" for intent method (#29000) Summary: Addresses https://github.com/facebook/react-native/issues/28934 ## 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 --- .../react/modules/intent/IntentModule.java | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java index 41383e528dc3c1..98377c76f7909f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java @@ -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) { @@ -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); + } } }