https://kotlinlang.org logo
#compose-android
Title
# compose-android
g

galex

11/26/2023, 12:53 PM
Hello, is there a known way to pass arguments to a Fragment via
AndroidViewBinding
? Obviously, as it is inflated by a View (Binding), it is inflated without arguments:
Copy code
AndroidViewBinding(factory = ComposeDashboardMainBinding::inflate) {
    val myFragment = composeDashboardContainerView.getFragment<DashboardMainFragment>()
                        
    myFragment.arguments = Bundle().apply {
       putString("test-param", "Alex") // Not passed before onCreate and others
    }

     myFragment.setParams("Alex") // Calling a function does work
}
I've found a way!
Copy code
AndroidView(factory = { context ->
    FrameLayout(context).apply {
        id = R.id.compose_test
        supportFragmentManager.beginTransaction()
            .add(R.id.compose_test, DashboardMainFragment().apply {
                arguments = Bundle().apply {
                    putString("test-param", "Alex")
                }
            }, "FRAGMENT_TEST")
            .commitNow()
    }
}, onRelease = {
    val fragment = supportFragmentManager.findFragmentByTag("FRAGMENT_TEST")
    fragment?.let { it1 -> supportFragmentManager.beginTransaction().remove(it1).commitNow() }
},
    modifier = Modifier.fillMaxSize()
)
3 Views