React-native inside a Fragment

后端 未结 3 1740
萌比男神i
萌比男神i 2020-12-08 03:03

How to Start react-native inside of a fragment? While putting react-native inside Fragment, onCreateView function is unable to return View from mReactRootView.

3条回答
  •  情话喂你
    2020-12-08 04:05

    There are libraries available that handle this for you.

    One that I use is react-native-android-fragment

    As per the instructions on the linked GitHub repository:

    1. Add the following line to your build.gradle compile 'com.github.hudl:react-native-android-fragment:v0.43.2'.

    e.g.

    allprojects {
      repositories {
        ...
        maven { url 'https://jitpack.io' }
      }
    }
    
    dependencies {
      // Version will correspond to its dependnecy on React Native
      compile 'com.github.hudl:react-native-android-fragment:v0.43.2'
    }
    
    1. Build you react code into the fragment

      Fragment reactFragment = new ReactFragment.Builder() .setComponentName("HelloWorld") .setLaunchOptions(launchOptions) // A Bundle of launch options .build();

    2. Place the Fragment in a FrameLayout that you would have in your XML layout file. In my case, the FrameLayout ID is react_holder.

      getSupportFragmentManager() .beginTransaction() .add(R.id.react_holder, reactFragment) .commit();

提交回复
热议问题