React Native 与 嵌入 Android 原生与 Activity 页面互相跳转 (1)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(createView());
}
private View createView() {
LinearLayout ll= new LinearLayout(this);
ll.setGravity(Gravity.CENTER);
ll.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// 设置文字
TextView mTextView = new TextView(this);
mTextView.setText("hello world");
LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 在父类布局中添加它,及布局样式
ll.addView(mTextView, mLayoutParams);
return ll;
}
第二步,创建 MyIntentModule 类,并继承 ReactContextBaseJavaModule。
注意:方法头要加 @ReactMethod
/**
原生 Activity 与 React 交互——模块
*/
public class MyIntentModule extends ReactContextBaseJavaModule {
public MyIntentModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "IntentMoudle";
}
//注意:记住 getName 方法中的命名名称,JS 中调用需要
@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());
}
}
@ReactMethod
public void dataToJS(Callback successBack, Callback errorBack){
try{
Activity currentActivity = getCurrentActivity();
String result = currentActivity.getIntent().getStringExtra("data");
if (TextUtils.isEmpty(result)){
result = "没有数据";
}
successBack.invoke(result);
}catch (Exception e){
errorBack.invoke(e.getMessage());
}
}
//注意:sta
rtActivityFromJS、dataToJS 方法添加 RN 注解(@ReactMethod),否则该方法将不被添加到 RN 中
}
第三步,创建 MyReactPackage 类
实现 ReactPackage 接口暴露给 RN 调用,在 createNativeModules 里注册上一步添加的模块:
/**
注册模块
*/
public class MyReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new MyIntentModule(reactContext));
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
第四步,在 MainApplication 中的 getPackages 方法中注册到 ReactPackage 中:
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new MyReactPackage()
);
}
第五步,接下来的工作便是 RN 的 Index.js 代码:
import React, { Component } from 'react';
import {
View,
NativeModules,
TouchableNativeFeedback,
ToastAndroid
} from 'react-native';
export default class App extends Component<{}> {
_onPressButton() {
console.log("You tapped the button!");
NativeModules
.IntentMoudle
.startActivityFromJS("com.myreactdemo.MyActivity", null);
}
render() {
return (
<View>
<TouchableNativeFeedback onPress={this._onPressButton}>
<Text>跳转到原生页面</Text>
</TouchableNativeFeedback>
</View>
);
}
}
评论