Skip to content

Commit 758af24

Browse files
swabbassyogevbd
authored andcommitted
Add support for getLaunchArgs on Android (#5466)
Add support for getLaunchArgs on Android **Usage:** 1. **Pass** args in order to run the application on the device using ADB or xcrun or Detox: - Detox: `device.launchApp(bundleId,{launchArgs: {'-key1': 'val1'}})` must use launchArgs key, see [docs](https://github.com/wix/Detox/blob/master/docs/APIRef.DeviceObjectAPI.md#7-additional-launch-arguments). - ADB: `adb shell am start -n com.yourpackage/com.yourpackage.YourActivity --es launchArgs "{some stringyfied json}"` - Xcrun: `/usr/bin/xcrun simctl launch ${udid} ${bundleId} --args -key1 value1 -key2 value2` 2. **Retrieve** the arguments: `const launchArgs = await Navigation.getLaunchArgs();` - There is a small difference between platforms on how they pass the launch args: - on iOS, the args will be passed as an array of pairs (argv,argc) - on Android, the args will be passed in the form of a dictionary Extract args as a dictionary example: ```javascript const launchArgs = await Navigation.getLaunchArgs(); let resultArgs = {}; if (Platform.OS === 'android') { resultArgs = launchArgs; // no need to proccess its a dictionary } else { for (let i = 0; i < launchArgs.length; i += 2) { if (i + 1 < launchArgs.length) { resultArgs[launchArgs[i]] = launchArgs[i + 1]; } } } ```
1 parent 1d335ba commit 758af24

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
55

6+
import com.reactnativenavigation.utils.LaunchArgsParser;
67
import com.facebook.react.ReactInstanceManager;
78
import com.facebook.react.bridge.Arguments;
89
import com.facebook.react.bridge.Promise;
@@ -69,6 +70,11 @@ public String getName() {
6970
return NAME;
7071
}
7172

73+
@ReactMethod
74+
public void getLaunchArgs(String commandId, Promise promise) {
75+
promise.resolve(LaunchArgsParser.parse(activity()));
76+
}
77+
7278
@ReactMethod
7379
public void getConstants(Promise promise) {
7480
ReactApplicationContext ctx = getReactApplicationContext();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.reactnativenavigation.utils;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
7+
import com.facebook.react.bridge.Arguments;
8+
import com.facebook.react.bridge.WritableMap;
9+
10+
public final class LaunchArgsParser {
11+
12+
private static final String LAUNCH_ARGS = "launchArgs";
13+
14+
/**
15+
* Parses launch args passed to activity intent to WritableMap
16+
* @param activity to fetch the extra launch args
17+
* @return parsed writable map if it exist, otherwise empty map will be returned
18+
*/
19+
public static WritableMap parse(Activity activity) {
20+
if (activity != null) {
21+
Intent intent = activity.getIntent();
22+
if (intent != null) {
23+
Bundle launchArgs = intent.getBundleExtra(LAUNCH_ARGS);
24+
if (launchArgs != null) {
25+
return Arguments.fromBundle(launchArgs);
26+
}
27+
}
28+
}
29+
return Arguments.createMap();
30+
}
31+
}

0 commit comments

Comments
 (0)