Advanced state and side effects in Jetpack Compose

Поділитися
Вставка
  • Опубліковано 3 жов 2024
  • In this workshop you will learn advanced concepts related to State and Side Effects APIs in Jetpack Compose. We'll see how to create a state holder for stateful composables whose logic isn't trivial and how to create coroutines and call suspend functions from Compose code. We'll learn what are side effects, and different APIs, like LaunchedEffect and rememberUpdatedState, to trigger them and accomplish different use cases.
    Resources:
    State in Jetpack Compose codelab → goo.gle/compos...
    Advanced State and Side Effects in Jetpack Compose codelab → goo.gle/3Gdhyn5
    Side-effects in Compose → goo.gle/3KN54oY
    UI state production pipeline → goo.gle/archit...
    Architecting your Compose UI → goo.gle/3UonhMN
    Consuming flows safely in Jetpack Compose → goo.gle/3GwleAS
    Other supported types of state → goo.gle/compos...
    Compose and other libraries → goo.gle/3ZUToor
    Save UI state in Compose → goo.gle/40Uw2kj
    Lifecycle of composable functions → goo.gle/3KsEPTB
    Thinking in Compose → goo.gle/3o2Ie40
    Splash screen API → goo.gle/3KKXZ8C
    Maps Compose → goo.gle/3GUfGAn
    When should I use derivedStateOf? → goo.gle/3GwY11i
    Now in Android → goo.gle/nia
    Speakers: Alejandra Stamato, Manuel Vicente Vivo
    Watch more:
    Watch all Android and Play Sessions → goo.gle/IO23_a...
    Watch all the workshops from Google I/O 2023 → goo.gle/IO23_w...
    Watch more Mobile Sessions → goo.gle/IO23_m...
    All Google I/O 2023 Sessions → goo.gle/IO23_all
    Subscribe to Android Developers → goo.gle/Androi...
    #GoogleIO#Featured #AndroidDevelopers #JetpackCompose

КОМЕНТАРІ • 38

  • @AndroidDevelopers
    @AndroidDevelopers  Рік тому +8

    To learn more about what's new in Android, check out the Android Keynote at #GoogleIO → goo.gle/IO23_androidkey_pin

    • @Himachaliyan
      @Himachaliyan Рік тому

      Hi @AndroidDevelopers , We are using compose for our application , we are group of 6 engineers working on the app, from all around the world,
      Our app is having very laggy behavior and need your help if you can so that we can follow the best practices, we have developed very big app on the compose and as we can see the laggy behavior, we are scratching our head,
      We have used good compose practice everything is in small set of composables, we using states, we are using coroutines,
      Can you please help us, is there any way we can get over a call, for some help

  • @AmitJayant
    @AmitJayant Рік тому +28

    Manuel: I'm Manuel - same team, same title
    Manuel making use of state very well 😄

  • @gorudonu
    @gorudonu 5 місяців тому +3

    I wonder how people who's written this code didn't look at it and said "this is way too over engineered, we should step back and think it over".

  • @frankensteingodzilla2444
    @frankensteingodzilla2444 2 місяці тому +3

    14:39 - rememberCoroutineScope
    17:16 - Creating a state holder
    29:46 - produceState
    35:10 - derivedStateOf

  • @technophile_
    @technophile_ Рік тому +5

    This talk, hands down, had the best flow ever! Great job Stamato and Manuel!! 👏🏻👏🏻❤

  • @tch.777
    @tch.777 Рік тому +2

    Thank you very much.
    That video was so important😊!!
    The new thing for me that I was searching for was the Saver for the rememberSaveable to survive configuration changes with my own non primitive type.
    I built a very buteful and customized calendar and needed that.
    Thank you!!!❤

  • @McMouse88
    @McMouse88 6 місяців тому

    "like so" sounds so cute ))
    Alejandra and Manuel thanks a lot for your work )

  • @chandra_83
    @chandra_83 Рік тому

    Great video. Good understanding of producestate,launchedeffect,coroutinescope

  • @dmorales8027
    @dmorales8027 Рік тому

    Muchas gracias por la informacion. Han dado el panorama completo de los efectos secundarios y estados avanzados, vi el codelab y me mareaba, viendo el video, fue como decimos aca en Peru, "directo a la vena", mucho mas didactico. Ya le toca a cad auno profundizar mas y ponerlos en practica. Muchas gracias!!!!

  • @gajendrasinghrathore5994
    @gajendrasinghrathore5994 6 місяців тому

    Thanks for sharing, it was helpful.

  • @leandroperalta4476
    @leandroperalta4476 Рік тому +1

    Gracias gente! Me llevo algunos conceptos nuevos después de ver este video. Muy bien por mencionar España y Argentina durante el tutorial. 😄Existe algún otro video o CodeLab que explique como escribir tests para este tipo de implementación?

  • @Oleksii-z7x
    @Oleksii-z7x 10 місяців тому

    Great video!

  • @ingenieroluisfer
    @ingenieroluisfer 9 місяців тому +1

    👋🏼🇨🇴🧔🏻👍🏻🤝🏻 Estupenda información

  • @AnnaMinienkova
    @AnnaMinienkova 10 місяців тому

    Thank you very much.

  • @abhijeetsingh115
    @abhijeetsingh115 20 днів тому

    Launched effect do get re-triggered when holding fragment is replaced. A to B and back to A. Is it still the best place to make network call?

  • @carlosdiestro6924
    @carlosdiestro6924 Рік тому +1

    Awesome guys! That was really helpful! I have a question though regarding the section for the InputText and its State:
    What are the differences between creating and declaring the state for the composable the way you do in the video and providing the state through the viewmodel? Being more specific:
    - Having a screen state in the viewmodel (as a data class and then wrapped with StateFlow) composed of the input text and hint
    - Collecting that state in the screen with the collectAsStateWithLifecycle API and passing the corresponding arguments to the InputText composable
    - Using an event call to the viewmodel to update the input text (this will call _state.update and make a copy of the screen state updating the value for input text)
    Is it a better practice to do it the way you show in the video when the information of that composable isn't dependable on the domain/data layer? However, this option delegates more "thinking" work to the UI rather than keeping it "dumb". What are your views about this?
    Thanks in advance and keep up the good work!

    • @AlejandraStamato
      @AlejandraStamato Рік тому +1

      Hoisting to the VM is fine, if you need to apply business logic to the TextField state for sure.
      But be careful as currently we do not recommend using a reactive stream (such as StateFlow) to represent the textfield. consider using mutable state in the viewmodel and avoid async validations. The full explanation is described in this blog medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5

  • @ellasaro
    @ellasaro Рік тому +1

    Hi! There is something that I can't quite get my head around. In step six, when talking about LaunchedEffect vs rememberCoroutineScope, it reads: "LaunchedEffect guarantees that the side-effect will be executed when the call to that composable makes it into the Composition. If you use rememberCoroutineScope and scope.launch in the body of the LandingScreen, the coroutine will be executed every time LandingScreen is called by Compose regardless of whether that call makes it into the Composition or not." - What would be the case for a Composable to be called but not "making it into the Composition"? Can a Composable function be called but not end up in the UI? How so? Thanks!

  • @GTEK0122
    @GTEK0122 6 місяців тому

    personal memo
    4:03 viewModel as single source of truth
    9:40 side-effect

  • @JerryStephenson-u2n
    @JerryStephenson-u2n Рік тому +2

    Great video!
    In the section about saving the input state you implemented a custom saver in the state object and use that in rememberSaveable. One way that I have approached this in the past is to make the state object parcelable using the kotlinx parcelize annotation. From that point I just have the initialization of this object wrapped in rememberSaveable and it is restored as expected through process death configuration changes. What are the benefits of creating a custom saver versus this other approach? Are they both valid or is the parcelable approach I described not as beneifical?

    • @AlejandraStamato
      @AlejandraStamato Рік тому

      For sure! That's actually the simplest way as per developer.android.com/jetpack/compose/state#ways-to-store
      In the video we show an alternative using listSaver if you can't add the plugin/annotate the class for any reason.

  • @hansonhuang6434
    @hansonhuang6434 6 місяців тому

    I'm confused about this bit, in which scenario that 'onTimeout' would be changed? feel like in practice, this lambda function would barely be changed. so didn't get the point of using "val currentOnTimeout by rememberUpdatedState(onTimeout)", which makes it look like that onTimeout would be changed quite often. Just wondering the scenarios? Thanks.

  • @frankensteingodzilla2444
    @frankensteingodzilla2444 4 місяці тому

    Hi, 9:01, why composable functions run multiple times, please?

  • @nzdas
    @nzdas 10 місяців тому

    In the ToDestinationUserInput composable I'm wondering why you chose to trigger a side effect to call the onToDestinationChanged lambda. Is there a reason why you can't simply use an if statement that invokes the lambda when the text is not the hint?

  • @ismaelsanchez3492
    @ismaelsanchez3492 Рік тому +18

    Very confusing as usual, Google's technologies looks like they are made on a contest of how to make things much more complex than they should. I don't need to show off how well I manage with complex technologies, I just need to be productive and make complex things simple, not otherwise. The probability of developers making mistakes are enormous using this APIs, unacceptable in my opinion.

    • @simbelarium
      @simbelarium Рік тому +2

      Completely agree with you on this, as time passes, Google makes thing more and more complex, and it seems that they simply wont stop.

    • @kirwakelvinkering3122
      @kirwakelvinkering3122 9 місяців тому +2

      Ooooh no , just do more on jetpack compose first , this was really helpful😢

    • @SumriseHD
      @SumriseHD 9 місяців тому +1

      ​@@kirwakelvinkering3122I agree. Once you get started with this stuff you see how it's actually pretty helpful.
      And yes, I agree with OP that it seems like a lot of it is pretty useless, and some of it definitely is, but a lot of other stuff definitely makes sense after a while.

    • @ShivamVermaokdone
      @ShivamVermaokdone 6 місяців тому +1

      okay then they tried to give you some value which can differentiate you from others who know only easy stuff!!!!!

    • @techocia
      @techocia 4 місяці тому

      Bro just tap on anything that says “Advanced” a bit later 😊

  • @dleonardo3238
    @dleonardo3238 10 місяців тому +1

    Was night light activated or what?

  • @frank4pickerson
    @frank4pickerson Рік тому +2

    Not very clear why derived state is used. Does the composable function recomposes if there is a calculation that checks if X>0?

  • @skarloti
    @skarloti Місяць тому

    I agree with most of the content of this video, but I think it needs to be corrected, especially after the middle.

  • @cetateanturmentat1200
    @cetateanturmentat1200 9 місяців тому

    I would really love to see how it compares with a different approach and why we should really use these. Crane app si a bad example, too big, too much noise. Less code but more examples would work better. I am still not very sure why would I use most of these.
    1. "Consuming a Flow safely..." what's the unsafe way? Why it is unsafe? What "safe" actually means in this context?
    2. Side effects seem more like an after thought than something organic. I try to avoid them as much as I can but maybe I'm wrong. Nevertheless this didn't quite help clarifying it.
    3. The language is very dogmatic: "better approach" but why? "safer" but why? Not very scientific. Sounds like an advertisement to me. Works for IO but if you really try learn things... go figure.

  • @shreekarki1771
    @shreekarki1771 4 місяці тому +1

    Disposable Effect??????

  • @JamesGoodwin86
    @JamesGoodwin86 6 місяців тому

    Too small for display on a mobile device