目前有一个老的android项目是用原生java语言写的,现在想集成react native,打算新的需求用js开发。
目前已经成功在项目里集成了react native,并且如果app启动时的activity是这个react native对应的activity的话,能正常启动并且展示正常。
但是如果在老的项目内加一个button,用startActivity的方式跳转到react native写的activity的话,没有报错,页面是空的,没有任何效果。
老的android项目内增加的button,跳转到新的用react native写的代码:
mBtnForward.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "打开新的页面");
Intent intent = new Intent();
intent.setClass(StartActivity.this, BaseReactActivity.class);
startActivity(intent);
}
});
AndroidManifest.xml配置
<activity
android:name=".activity.BaseReactActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
</activity>
BaseReactActivity的代码
package com.sitech.ac.activity;
import com.facebook.react.ReactActivity;
public class BaseReactActivity extends ReactActivity {
@Override
protected String getMainComponentName() {
return "HelloWorld";
}
}
App.js
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button
} from 'react-native';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Button
title="你好"
color="red"
accessibilityLabel="Learn more about this purple button"
/>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
测试
</Text>
<Text style={styles.instructions}>
{instructions}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'yellow',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
刚才又重新试了一下,应该是bundle服务器自动掉了,没有加载到js导致的。但是为什么bundle会自动掉了呢? windows平台。