Skip to content

Commit

Permalink
Add support for getLaunchArgs on Android (#5466)
Browse files Browse the repository at this point in the history
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];
        }
      }
    }
```
  • Loading branch information
swabbass authored and guyca committed Sep 12, 2019
1 parent 968a46f commit 16646e7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativenavigation.react;

import com.reactnativenavigation.utils.LaunchArgsParser;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -72,6 +73,11 @@ public String getName() {
return NAME;
}

@ReactMethod
public void getLaunchArgs(String commandId, Promise promise) {
promise.resolve(LaunchArgsParser.parse(activity()));
}

@ReactMethod
public void getConstants(Promise promise) {
ReactApplicationContext ctx = getReactApplicationContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.reactnativenavigation.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;

public final class LaunchArgsParser {

private static final String LAUNCH_ARGS = "launchArgs";

/**
* Parses launch args passed to activity intent to WritableMap
* @param activity to fetch the extra launch args
* @return parsed writable map if it exist, otherwise empty map will be returned
*/
public static WritableMap parse(Activity activity) {
if (activity != null) {
Intent intent = activity.getIntent();
if (intent != null) {
Bundle launchArgs = intent.getBundleExtra(LAUNCH_ARGS);
if (launchArgs != null) {
return Arguments.fromBundle(launchArgs);
}
}
}
return Arguments.createMap();
}
}

0 comments on commit 16646e7

Please sign in to comment.