Search code examples
androidandroid-fragmentsandroid-jetpack-composekoinlanguage-interoperability

AndroidView of compose gives:: error No view found for id 0x7f0a01d2


I have been using Jetpack Compose and koin. I am facing error as below

java.lang.IllegalArgumentException: No view found for id 0x7f0a01d2 (com.pyronixhc2.android.app:id/fragment_container_view) for fragment GridItemFragment{9f95276} (fa3cd86f-79ea-41eb-b4a6-42be50a0aa1f id=0x7f0a01d2 tag=J76002662) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:547) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1839) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782) at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:565)

My implementation is as following::

@Composable
fun FragmentInComposeExample(device:Device){
AndroidView(factory = { context ->
        FragmentContainerView(context).apply {
            id = ViewCompat.generateViewId()
        }

    }, update = {
        (context as ScopeActivity).supportFragmentManager.commit {
            container?.let { it1 -> add(GridItemFragment.newInstance(device), device.deviceSerial) }
        }
    })
}

Fragment as Below::

    class GridItemFragment : Fragment() {
     private const val ARG_LOCAL_CAMERA = "param1"

   
     override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            arguments?.let {
                hikCamera = it.getSerializable(ARG_LOCAL_CAMERA) as Device?
            }
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_grid_item, container, false)
        }

     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            val textV = view.findViewById<TextView>(R.id.link)
            textV.text = device?.deviceSerial ?: "no device"
     }

    companion object {
            @JvmStatic
            fun newInstance(param1: Device =
                GridItemFragment().apply {
                  
                    arguments = Bundle().apply {
                        putSerializable(ARG_LOCAL_CAMERA, param1)
                    }
                }
        }
}

How to import fragment in compose method. I am calling FragmentInCompose(device:Device) from composable Screen


Solution

  • Here, I have taken different approach, rather than getting instance of fragment while adding it to Transition, instatiated at before and pass it into transition with its hashCode as below

    @Composable
    fun FragmentInComposeExample(fragment:Fragment){
    AndroidView(
             modifier = Modifier.fillMaxSize(),
             factory = { context ->
                 FrameLayout(context).apply {
                     id = viewId
                 }
             },
             update = { view ->
                  if(fragmentManager.findFragmentByTag(fragment.hashCode().toString()) == null) {
                         fragmentManager.beginTransaction()
                             .add(view.id, fragment, fragment.hashCode().toString())
                             .commitNow()
                     }
             }
         )
    }
    

    Its work with me for Videos of different device's live feedin gride.