Skip to content

Commit 04fe3ed

Browse files
Krzysztof Borowyfacebook-github-bot
Krzysztof Borowy
authored andcommitted
Fix: Set "new task flag" for intent method (#29000)
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
1 parent a8cd8f7 commit 04fe3ed

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java

+23-19
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,8 @@ public void openURL(String url, Promise promise) {
8686
}
8787

8888
try {
89-
Activity currentActivity = getCurrentActivity();
9089
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url).normalizeScheme());
91-
92-
String selfPackageName = getReactApplicationContext().getPackageName();
93-
ComponentName componentName =
94-
intent.resolveActivity(getReactApplicationContext().getPackageManager());
95-
String otherPackageName = (componentName != null ? componentName.getPackageName() : "");
96-
97-
// If there is no currentActivity or we are launching to a different package we need to set
98-
// the FLAG_ACTIVITY_NEW_TASK flag
99-
if (currentActivity == null || !selfPackageName.equals(otherPackageName)) {
100-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
101-
}
102-
103-
if (currentActivity != null) {
104-
currentActivity.startActivity(intent);
105-
} else {
106-
getReactApplicationContext().startActivity(intent);
107-
}
90+
sendOSIntent(intent, false);
10891

10992
promise.resolve(true);
11093
} catch (Exception e) {
@@ -235,6 +218,27 @@ public void sendIntent(String action, @Nullable ReadableArray extras, Promise pr
235218
}
236219
}
237220

238-
getReactApplicationContext().startActivity(intent);
221+
sendOSIntent(intent, true);
222+
}
223+
224+
private void sendOSIntent(Intent intent, Boolean useNewTaskFlag) {
225+
Activity currentActivity = getCurrentActivity();
226+
227+
String selfPackageName = getReactApplicationContext().getPackageName();
228+
ComponentName componentName =
229+
intent.resolveActivity(getReactApplicationContext().getPackageManager());
230+
String otherPackageName = (componentName != null ? componentName.getPackageName() : "");
231+
232+
// If there is no currentActivity or we are launching to a different package we need to set
233+
// the FLAG_ACTIVITY_NEW_TASK flag
234+
if (useNewTaskFlag || currentActivity == null || !selfPackageName.equals(otherPackageName)) {
235+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
236+
}
237+
238+
if (currentActivity != null) {
239+
currentActivity.startActivity(intent);
240+
} else {
241+
getReactApplicationContext().startActivity(intent);
242+
}
239243
}
240244
}

0 commit comments

Comments
 (0)