在Android Presentation中嵌入React Native内容无法显示的问题求助
在Android Presentation中嵌入React Native内容无法显示的问题求助
各位好,我现在遇到一个React Native嵌入Android Presentation的问题,想请大家帮忙排查下:
我已经完成了Android端RN的基础配置,用标准的ReactActivity可以正常显示RN内容,这说明我的RN项目本身配置是没问题的。但我现在需要把RN内容放到Presentation(多屏场景下的副屏展示组件)里,自己写了实现代码后,RN内容完全不渲染,屏幕只显示我设置的红色背景,没有任何RN的界面元素。
以下是我实现的MyReactPresentation类代码:
class MyReactPresentation(context: Context, display: Display): Presentation(context, display), DefaultHardwareBackBtnHandler { private val root: ReactRootView = ReactRootView(context).apply { setBackgroundColor(Color.RED) layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) } private val reactManager = (context.applicationContext as MainApplication).reactNativeHost.reactInstanceManager private val TAG = "MyReactPresentation" init { window?.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(root) val handler = object : ReactInstanceEventListener { override fun onReactContextInitialized(context: ReactContext) { reactManager.removeReactInstanceEventListener(this) root.startReactApplication(reactManager, "MyReactPresentation", null) Log.i(TAG, "React application started") reactManager.onHostResume(ownerActivity, this@MyReactPresentation) } } reactManager.addReactInstanceEventListener(handler) if (!reactManager.hasStartedCreatingInitialContext()) { reactManager.createReactContextInBackground() } else { val currentContext = reactManager.currentReactContext if (currentContext != null) { Log.i(TAG, "React context already exists, starting app directly") root.startReactApplication(reactManager, "MyReactPresentation", null) reactManager.onHostResume(ownerActivity, this) } } Log.i(TAG, "onCreate completed") } override fun onAttachedToWindow() { super.onAttachedToWindow() Log.i(TAG, "onAttachedToWindow") val currentContext = reactManager.currentReactContext if (currentContext != null) { reactManager.onHostResume(ownerActivity, this) } } override fun onDetachedFromWindow() { super.onDetachedFromWindow() Log.i(TAG, "onDetachedFromWindow") val currentContext = reactManager.currentReactContext if (currentContext != null) { reactManager.onHostPause(ownerActivity) } } override fun dismiss() { Log.i(TAG, "dismiss") val currentContext = reactManager.currentReactContext if (currentContext != null) { reactManager.onHostDestroy(ownerActivity) } super.dismiss() } override fun invokeDefaultOnBackPressed() { super.onBackPressed() } }
从logcat里看,没有任何错误日志,只有一堆类似下面的警告:
W Could not find generated setter for class com.facebook.react.views.debuggingoverlay.DebuggingOverlayManager W Could not find generated setter for class com.facebook.react.uimanager.LayoutShadowNode W Could not find generated setter for class com.facebook.react.views.drawer.ReactDrawerLayoutManager W Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollViewManager W Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollContainerViewManager W Could not find generated setter for class com.facebook.react.views.progressbar.ReactProgressBarViewManager W Could not find generated setter for class com.facebook.react.views.progressbar.ProgressBarShadowNode W Could not find generated setter for class com.facebook.react.views.scroll.ReactScrollViewManager W Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchManager W Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchShadowNode W Could not find generated setter for class com.facebook.react.views.safeareaview.ReactSafeAreaViewManager W Could not find generated setter for class com.facebook.react.views.safeareaview.ReactSafeAreaViewShadowNode W Could not find generated setter for class com.facebook.react.views.swiperefresh.SwipeRefreshLayoutManager W Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageViewManager W Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageShadowNode W Could not find generated setter for class com.facebook.react.views.image.ReactImageManager W Could not find generated setter for class com.facebook.react.views.modal.ReactModalHostManager W Could not find generated setter for class com.facebook.react.views.text.ReactRawTextManager W Could not find generated setter for class com.facebook.react.views.text.ReactRawTextShadowNode W Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputManager W Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputShadowNode W Could not find generated setter for class ...(剩余类似警告省略)
我已经确认这些警告在使用ReactActivity的时候也会出现,但完全不影响RN内容渲染,所以应该不是导致Presentation里不显示的原因。代码里也已经开启了硬件加速,日志里能看到"React application started"和"onCreate completed"的打印,说明RN的初始化流程是正常走到了,但界面上就是没有RN内容。
有没有朋友遇到过类似的问题?或者能帮我看看我的Presentation实现里有没有什么遗漏的地方?
内容来源于stack exchange
相关产品推荐
相关产品推荐

