make fragment an UI Component in react-native error

Viewed 342

I have a problem when I attempt to make fragment an UI Component.

Code below works well when first render in react native, but when the component re-render App will crash due to transaction?.replace(R.id.id_empty_content, EmptyFragment(), "EmptyFragment").The error shows no view for id_empty_content for fragment.Is fragment not destoryed when component unamount but the id_empty_content has disappeared? Hope someone can help me to figure this out.

ViewManager:

class EmptyFragmentManager : SimpleViewManager<RelativeLayout>() {

    override fun createViewInstance(reactContext: ThemedReactContext?): RelativeLayout{
        val manager = reactContext?.currentActivity?.fragmentManager
        val transaction = manager?.beginTransaction()
        val view = LayoutInflater.from(reactContext).inflate(R.layout.layout_empty, null, false) as RelativeLayout
        transaction?.replace(R.id.id_empty_content, EmptyFragment(), "EmptyFragment")
        transaction?.commit()
        return view
    }

    override fun getName(): String = "Empty"

}

layout_empty.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/id_empty_content"></FrameLayout>
</RelativeLayout>

useage:

import React, { Component } from 'react'
import { StyleSheet, Platform } from 'react-native'
import { Container, } from 'native-base'

import { colors, constants } from '../config'
import { SessionList, Empty } from '../components/sdk'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    position: 'relative',
    backgroundColor: colors.white,
  },
  session_list: {
    flex: 1,
    marginTop: constants.STATUS_BAR_HEIGHT,
  },
})

export class InfoListPage extends Component {
  render () {
    return (
      <Container style={styles.container}>
        {Platform.OS === 'ios' ? null : <Empty style={styles.session_list}/>}
      </Container>
    )
  }
}

error:

    java.lang.IllegalArgumentException: No view found for id 0x7f100acf (cn.samuel.pro:id/id_empty_content) for fragment EmptyFragment{4d538b #0 id=0x7f100acf EmptyFragment}
0 Answers
Related