android - 如何将 React Native 添加到我的应用程序中?(安卓)
问题描述
我目前正在做一些比较完整react-native app
和android/iOS native + react-native app
. 从公司的角度来看,看看我们可以在多大程度上增加这种方法所产生的价值,这将是很有趣的。
我的问题是,我们如何膨胀,例如,在 android 原生片段或活动中的 react-native 表单。
我希望,通过这种方式,android/iOS 原生开发人员将只专注于他们各自操作系统的适当菜单,以及要在react-native
.
我遵循了这个官方文档:https ://facebook.github.io/react-native/docs/integration-with-existing-apps
在这个例子中,我尝试在类上扩展 React-Native 代码MyReactActivity.java
。
MyReactActivity.java
public class MyReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModulePath("index")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "myreactnativeapp", null);
setContentView(mReactRootView);
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
if (mReactRootView != null) {
mReactRootView.unmountReactApplication();
}
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private final int OVERLAY_PERMISSION_REQ_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
}
}
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyReactActivity.class);
startActivity(intent);
}
});
}
输出(在yarn start
控制台上运行后)
先感谢您!
解决方案
推荐阅读
- react-native - 当 TextInput 上的 editable={false} 时滚动不起作用。仅限安卓
- python - 无法在文本文件中写入多个字段
- r - 相当于选择 i.*
- javascript - 如何使用 foreach 将值设置为数组
- javascript - 如何从html向js发送数据
- performance - 解释器语言比编译器语言慢的主要原因是什么?
- java - Flink : 以集群模式加载资源文件
- azure - 禁用/删除 Azure Boards Team Board
- javascript - 使用 moment.js 的日期格式
- c# - 将 JSON 值存储到具有有序键名的字典中