Bryan Lee

    Bryan Lee

    2 years ago
    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.
    Adam Powell

    Adam Powell

    2 years ago
    I've done it by creating a
    ComposableFragment
    that hosts compose content as its view; works pretty well
    Bryan Lee

    Bryan Lee

    2 years ago
    Is this out in dev03 or do we have to wait for a later release?
    Adam Powell

    Adam Powell

    2 years ago
    basically just this:
    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?)
    }
    Bryan Lee

    Bryan Lee

    2 years ago
    That works great, thanks.