https://kotlinlang.org logo
#compose
Title
# compose
m

Merhawifissehaye

06/12/2020, 8:30 PM
How do I use compose inside a Fragment?
setContent
doesn't seem to be available outside of an Activity? I also learned in a comment inside this stackoverflow question that there is a work in progress to add support for Fragments. Are there any updates on that? Or else what options do I have to be able to use compose inside fragments?
z

Zach Klippenstein (he/him) [MOD]

06/12/2020, 8:44 PM
You can set a
FrameLayout
as your content view and then call
setContent
on the
FrameLayout
.
1
☝️ 2
a

Andrey Kulikov

06/12/2020, 9:05 PM
yes. it is basically just
abstract class ComposableFragment : Fragment() {
Copy code
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FrameLayout(requireContext()).apply {
        setContent(Recomposer.current()) {
            Content()
        }
    }
Copy code
@Composable
    abstract fun Content()
}
👍🏼 1
👍 2
but we are still considering to add it in the library to make gradual migration as simple as possible
j

Javier

06/12/2020, 9:23 PM
@Andrey Kulikov compose will not be based on just a single activity and composable functions?
l

Leland Richardson [G]

06/12/2020, 9:27 PM
@Javier you can do the single activity/composable approach, but it won’t be required as this would prevent a lot of people from easily migrating
👍 1
j

Javier

06/12/2020, 9:28 PM
@Leland Richardson [G] the jetpack navigation library will include the single activity with no fragments approach?
l

Leland Richardson [G]

06/12/2020, 9:30 PM
the compose extension to the navigation arch component library will allow for routes to be defined by a composable function. it will allow other routes to also be defined by fragments if you have them (for easy incremental migration). If you only have composable routes, it is not yet clear if it will end up using fragments as an implementation detail or not, but i think there’s a decent chance that it won’t
👍 2
but in any event, it should just be an implementation detail
there may at some point be a way to define your navigation graph from inside of a composable (similar to the open source compose-router library’s API) but these areas are still being explored by our team since we want to make sure that we believe it is a good approach before releasing something like that
j

Javier

06/12/2020, 10:03 PM
Thank you for all the info :)
m

Merhawifissehaye

06/13/2020, 10:41 AM
Thank you all. Could compose by any chance cause problem when used along with data binding. There is another part of the code that uses data binding. I am getting an error related to data binding after installing compose. I have created a gist of the stack trace here. I am still struggling to make compose work with my existing project. Any clue would be appreciated. I have added the following to my build.gradle
Copy code
composeOptions {
        kotlinCompilerVersion "1.3.70-dev-withExperimentalGoogleExtensions-20200424"
        kotlinCompilerExtensionVersion "0.1.0-dev13"
    }
I am on a multi module project, and I have added the option to both build.gradle files.
89 Views