如何在启动React Native Activity时发送数据?

时间:2018-03-13 18:15:50

标签: android react-native

我想发送数据以在发布时对原生活动做出反应,我在android活动ReactContext中显示null,显示订单列表,在项目点击时我要打开详细视图在reactNative活动中的顺序。但我无法发送数据,因为我在每个活动生命周期回调中获得 @Override public void onItemClick(Order order) { Intent intent = new Intent(MainActivity.this, ReactNativeActivity.class); intent.putExtra("order",order); startActivityForResult(intent, 9); } public class ReactNativeActivity extends ReactActivity { private Order order; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); order = (Order) getIntent().getSerializableExtra("order"); WritableMap map = new WritableNativeMap(); map.putBoolean("complete", order.isCompleted()); map.putString("id", order.getId()); //Here I am getting null reactcontext sendEvent(getReactInstanceManager().getCurrentReactContext(), "order", map); } private void sendEvent(ReactContext reactContext, String eventName, WritableMap params) { reactContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(eventName, params); } public Order getOrder() { return order; } @Override protected String getMainComponentName() { return "JioAssignment"; } public void setResult(boolean isCompleted, String id) { Intent intent = new Intent(); intent.putExtra("completed", isCompleted); intent.putExtra("id", id); setResult(9, intent); finish(); } }

按如下方式启动React活动:

public class MainApplication extends Application implements ReactApplication {

private CustomReactPackage mCustomPackage;
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
        mCustomPackage =  new CustomReactPackage();
        return Arrays.<ReactPackage>asList(
                new MainReactPackage(), mCustomPackage

        );
    }

    @Override
    protected String getJSMainModuleName() {
        return "index";
    }
};

@Override
public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
}

public CustomReactPackage getmCustomPackage() {
    return mCustomPackage;
}

@Override
public void onCreate() {
    super.onCreate();
    SoLoader.init(this, false);
}
}

ReactVativeActivity:

public class CustomReactPackage implements ReactPackage {

private Bridge mBridge;
private ReactApplicationContext reactContext;

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    this.reactContext = reactContext;
    mBridge = new Bridge(reactContext);

    modules.add(mBridge);
    return modules;

}

public Bridge getmBridge() {
    return mBridge;
}

public ReactApplicationContext getReactContext() {
    return reactContext;
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}
}

ApplicationClass:

public class Bridge extends ReactContextBaseJavaModule {
private Order order;

public Bridge(ReactApplicationContext reactContext) {
    super(reactContext);
}

@Override
public String getName() {
    return "Bridge";
}

@Nullable
@Override
public Map<String, Object> getConstants() {

    Map<String, Object> vmap = new HashMap<>();
    vmap.put("completed", ((ReactNativeActivity) getCurrentActivity()).getOrder().isCompleted());
    vmap.put("id", ((ReactNativeActivity) getCurrentActivity()).getOrder().getId());
    return vmap;
}

private void sendEvent(
                       String eventName,
                       WritableMap params) {
    getReactApplicationContext()
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(eventName, params);
}

@ReactMethod
public void updateOrder(boolean isCompleted, String id) {
    ((ReactNativeActivity) getCurrentActivity()).setResult(isCompleted, id);
    Log.w("update", id);
}


}

CustomePackage:

$path = '\\L04274\C$\Temp' 
$exclusion = @(Get-Content -LiteralPath '\\L04274\C$\Temp\Exclusions.csv')

 (Get-ChildItem -Path $path -Force -Exclude $exclusion | ForEach-Object -Process { 
  $allowed = $true 
  foreach ($exclude in $exclusion) { 
     if ( $_.FullName  -like $exclude) {  
       $allowed = $false
       break
    }

  }
  if ($allowed) {
    $_ 
  }
 }).FullName

ReactContextBaseJavaModule:

YAR

我已经通过了几个例子,但没有找到任何有效的例子。请帮忙。

1 个答案:

答案 0 :(得分:0)

React native接受来自托管ReactContext的活动的props。创建一个ReactActivityDelegate并将其分配给您的ReactActivity。

public class ReactNativeActivityDelegate extends ReactActivityDelegate {
    private Bundle initialProps = null;
    private Activity activity;

    public ReactNativeActivityDelegate(Activity activity, @Nullable String mainComponentName) {
        super(activity, mainComponentName);
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(activity != null) {
            Bundle bundle = activity.getIntent().getExtras();
            if(bundle != null) {
                initialProps = bundle;
            }
        }
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    protected Bundle getLaunchOptions() {
        return initialProps;
    }
}

像这样分配反应活动

public class ReactNativeBaseActivity extends ReactActivity {

    @Override
    protected String getMainComponentName() {
        return ReactNativeActivityDelegate.REACT_NATIVE_COMPONENT_NAME;
    }

    @Override
    protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactNativeActivityDelegate(this, getMainComponentName());
    }
}

像往常一样将捆绑包传递到活动中。传递的包可以在ReactNative道具中访问。 这段代码正在制作中,我们使用下面的代码。

String initialRoute = aOrBView() ? 
ReactNativeActivityDelegate.INITIAL_ROUTE_A_VIEW : 
ReactNativeActivityDelegate.INITIAL_ROUTE_B_VIEW;

nextIntent.putExtra(ReactNativeActivityDelegate.EXTRA_INITIAL_ROUTE, initialRoute);
startActivity(nextIntent);

然后我们在ReactNative中使用它......就像这样......

let initialRouteId = this.props.initialRoute && 
this.props.initialRoute.length > 0 ? this.props.initialRoute : 'DefaultRoute'
    return (
      <Navigator ....

我希望这会有所帮助......快乐的编码。