Slide 1

Slide 1 text

¿Fragments in Compose? Dinorah Tovar Google Developer Expert Android @ddinorahtovar @ddinorahtovar

Slide 2

Slide 2 text

Left Aligned Title Compose is a new cool way to write UI First let’s talk about compose @Composable fun view(data: Int) data: Int Composition emit

Slide 3

Slide 3 text

Simple quote or statement goes here. Ideally limit to four or five lines max. Composable functions Kotlin 100% @Composable fun SomeTextView( text: String ) { Text( text = text, style = DesignTheme.typography.body4, color = DesignTheme.colors.onSurface, textAlign = TextAlign.Start ) }

Slide 4

Slide 4 text

So - for a View We use Activity class HomeActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ComposableHolder() } } }

Slide 5

Slide 5 text

Left Aligned Title Today - some of us has this structure Second, let’s talk about fragments Activity Fragment ViewModel Simples flow

Slide 6

Slide 6 text

Left Aligned Title Activity Fragment ViewModel Activity Fragment ViewModel Fragment ViewModel Full App Today - some of us has this structure Second, let’s talk about fragments

Slide 7

Slide 7 text

Fragments with compose does not have setContent{} class OtherFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { return ComposeView(requireContext()).apply { setViewCompositionStrategy( DisposeOnViewTreeLifecycleDestroyed ) setContent { // View } } } }

Slide 8

Slide 8 text

The lifecycle is one of the most complex aspects of developing applications and has always been a source of bugs, and difficult to understand for developers.

Slide 9

Slide 9 text

Many different ideas Many task to handle Many blog post

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Fragments are not bad - but they probably can be better

Slide 15

Slide 15 text

Left Aligned Title Compose solve probably one of the hardest task in Android Compose to the rescue First Interaction Composition Insert “A” Insert “B” Second Interaction Re-composition

Slide 16

Slide 16 text

Left Aligned Title Compose is designed for State Hoisting - decoupling everything Compose to the rescue @Composable fun View() @Composable fun View() @Composable fun ViewContent()

Slide 17

Slide 17 text

This is also know As Uni- directional Data flow Left Aligned Title @Composable fun View() @Composable fun View() @Composable fun ViewContent() State Event

Slide 18

Slide 18 text

Left Aligned Title Compose is designed for State Hoisting Compose to the rescue @Composable State Holder ViewModel/ Presenters Access to 
 Logic UI component state UI component Composition

Slide 19

Slide 19 text

Left Aligned Title Example - State Hoisting @Composable fun View(){ var variable by remember { mutableStateOf("") } val onValueChange = { text -> variable = text } Component( name= variable, onValueChange= onValueChange ) } @Composable fun ViewContent( name : String, onValueChange : (String) -> Unit ){ // Your view }

Slide 20

Slide 20 text

Composable function Can access ViewModel @Composable private fun ComposableHolder( viewModel: MyViewModel = viewModel() ) { val uiState by viewModel.uiState.collectAsState() when { uiState.isLoading -> { /* ... */ } uiState.isSuccess -> { /* ... */ } uiState.isError -> { /* ... */ } } }

Slide 21

Slide 21 text

Does not requiere Fragments At the end of the day - using Compose AT ALL

Slide 22

Slide 22 text

Compose offers something different to everything we have seen - Give it a try (and decide if your fragments worth the effort)

Slide 23

Slide 23 text

¿Fragments in Compose? Dinorah Tovar Google Developer Expert Android @ddinorahtovar @ddinorahtovar