https://kotlinlang.org logo
b

Bryan Lee

12/31/2019, 9:51 PM
If trying to integrate Compose into existing applications that use something like Single Activity Multiple Fragments, would you continue with creating new fragments when a new view/screen is needed and just inflating a composable view with setContent{} instead of an xml layout? Or would you end up creating a 'root' fragment somewhere that hosts navigation for any new composable views, similar to how JetNews uses JetNewsApp & Status? Sorry if that doesn't really make sense. I'll try to clarify if needed.
a

Adam Powell

12/31/2019, 10:02 PM
I've done it by creating a
ComposableFragment
that hosts compose content as its view; works pretty well
b

Bryan Lee

12/31/2019, 10:04 PM
Is this out in dev03 or do we have to wait for a later release?
a

Adam Powell

12/31/2019, 10:14 PM
basically just this:
Copy code
abstract class ComposableFragment : Fragment() {
    final override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = FrameLayout(container?.context ?: requireContext()).apply {
        setContent {
            Content(savedInstanceState)
        }
        layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
    }

    final override fun onDestroyView() {
        super.onDestroyView()
        (view as? ViewGroup)?.setContent { }
    }

    @Composable
    abstract fun Content(savedInstanceState: Bundle?)
}
b

Bryan Lee

12/31/2019, 10:30 PM
That works great, thanks.
👍 1
2 Views