从React Native 跳转到原生页面

温故知新,记录一下吧,免得自己忘了

1.MyIntentModule.Java 类

public class MyIntentModule extends ReactContextBaseJavaModule {

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

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

  /**
  * 从JS页面跳转到原生activity 同时也可以从JS传递相关数据到原生
  * @param name 需要打开的Activity的class
  * @param params
  */
  @ReactMethod
  public void startActivityFromJS(String name, String params){
  try{
  Activity currentActivity = getCurrentActivity();
  if(null!=currentActivity){
  Class toActivity = Class.forName(name);
  Intent intent = new Intent(currentActivity,toActivity);
  intent.putExtra("params", params);
  currentActivity.startActivity(intent);
  }
  }catch(Exception e){
  throw new JSApplicationIllegalArgumentException(
  "不能打开Activity : "+e.getMessage());
  }
  }
}

2.MyReactPackage.java类

public class MyReactPackage implements ReactPackage {
  @Override
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
  return Arrays.<NativeModule>asList(
  new MyIntentModule(reactContext)

  );
  }

  @Override
  public List<Class<? extends JavaScriptModule>> createJSModules() {
  return Collections.emptyList();
  }

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

  }
}

3.MainApplication添加

new MyReactPackage()

4.随便创建一个Activity页面

(com.wildwolf.myrn.ThridActivity )

5.index.android.js

import {
...
  NativeModules
  } from 'react-native';
...
<TouchableWithoutFeedback
  onPress={() =>
  NativeModules.IntentModule.startActivityFromJS("com.wildwolf.myrn.ThridActivity","我是从JS传过来的参数信息.")}>
  <Text style={styles.text}>Click me.</Text>
</TouchableWithoutFeedback>

over

推荐阅读更多精彩内容