State in Jetpack Compose

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • Discover the core concepts of using state in Jetpack Compose by building a wellness app. Learn how the app's state determines what is displayed in the UI, how Compose keeps the UI updated when state changes, how to optimize the structure of your composable functions, and work with ViewModels in a Compose app.
    Resource: Using state in Jetpack Compose → goo.gle/3ssNnBO
    Speakers: Alejandra Stamato, Manuel Vicente Vivo
    Watch more:
    All Google I/O 2022 Sessions → goo.gle/IO22_AllSessions
    Android and Play at Google I/O 2022 playlist → goo.gle/IO22_AndroidDevs
    All Google I/O 2022 workshops → goo.gle/IO22_Workshops
    Subscribe to Android Developers → goo.gle/AndroidDevs
    #GoogleIO #Featured #AndroidDevelopers
  • Наука та технологія

КОМЕНТАРІ • 108

  • @jonathancampos8929
    @jonathancampos8929 2 роки тому +23

    Wow! Thanks for this presentation, the speakers did an incredible job together. Excited to watch new content with you two folks

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      Hey, Jonathan! We think so too! It's always a pleasure to see how excited viewers are for more content 🙂

    • @hindpal3551
      @hindpal3551 2 роки тому

      @@AndroidDevelopers p☺️

  • @isabelporto5243
    @isabelporto5243 2 роки тому +8

    Ale you are the best! Thank you so much for the presentation.

  • @ChicoRasia_CLabs
    @ChicoRasia_CLabs Рік тому +4

    Great job! I've been getting even more out of the codelabs that have this type of presentation. It helps to draw attention to the important details in the step by step guide. Keep it up!

  • @anibalbastiass
    @anibalbastiass 2 роки тому +7

    Great workshop! I really like the clear explanation and solved my doubts about the Composable States. You rock guys!

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      Thanks for the wonderful feedback; we're glad it was helpful! 😁

  • @chtiboss78
    @chtiboss78 2 роки тому +16

    Great presentation.
    I like the cristal clear workshop, common developer errors, and dynamism of the presentation.
    I am impatient to see another presentation like this, based on codelabs.

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      Happy to hear that the tutorial was helpful, Chti. You can also check out the codelab for Using state in Jetpack Compose, as seen in the video here: goo.gle/3ssNnBO

  • @JahangirJadi
    @JahangirJadi 2 роки тому +10

    Great workshop. Most of my concepts are cleared now. Thanks to both.

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      Happy to hear the tutorial was helpful, Jahangir. You can also check out the following link to follow along with our Jetpack Compose codelab: goo.gle/3ssNnBO

  • @antoniopinho2071
    @antoniopinho2071 2 роки тому +8

    Amazing presentation! What a star Ale! Thank you.

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому

      Hi António! We couldn't agree more. Thanks for watching✨

  • @ZachKlippenstein
    @ZachKlippenstein 2 роки тому +25

    I like that Manuel’s intro was stateful 😂

  • @YairCarreno
    @YairCarreno 2 роки тому +6

    Great workshop!. Thanks Alejandra and Manuel 🥳

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      We're delighted that you've enjoyed the lesson, Yair. Thanks for the support!

  • @seksky333
    @seksky333 2 роки тому +4

    I found what Manual said at the end about only passing the view model on screen-level composable not other composable functions was really useful.

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому +1

      Thanks for your feedback, Ches! Don't forget you can view the codelab for Using state in Jetpack Compose here: goo.gle/3ssNnBO

    • @seksky333
      @seksky333 2 роки тому +1

      @@AndroidDevelopers Yah, I've already completed the codelab and am using it as my reference for my new Android role in an eCommerce business. Looking forward to see more great features from Compose.

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

    I truly didn't know how to thank you for giving this to us for free.

  • @fernandosproviero1663
    @fernandosproviero1663 2 роки тому +5

    Great video! Congrats Alejandra and Manuel! Looking forward for more explanations like this :]

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

      We're delighted to know you enjoyed this video from Alejandra and Manuel!
      We love hearing feedback from our community members 🙂
      If you liked this, don't forget to check out more Google I/O 2022 Sessions linked below → goo.gle/IO22_AllSessions

  • @dhananjaykulkarni6102
    @dhananjaykulkarni6102 2 роки тому +1

    Yes I did code along. This video solved my big problem of not maintaining state of switch. Thanks a lot.

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

      We're happy to hear this, Dhananjay! If you'd like to learn more about Jetpack Compose, check the following codelab: goo.gle/3ssNnBO 😀

  • @lsbrujah
    @lsbrujah 2 роки тому +11

    Thanks for the presentation, amazing duo.

  • @user-vk3tb7rg7z
    @user-vk3tb7rg7z 5 місяців тому

    After working 6 months in compose. Today I Understand how to properly handle UI state in Compose . Thanks to the dev team 👍. Previously I violate the single source of truth by passing ViewModel down to composables.

  • @apvasanth031
    @apvasanth031 2 роки тому +1

    Thanks for the great workshop.

  • @pantelischas9460
    @pantelischas9460 2 роки тому +2

    nice video, it is helpful to understand the state in compose

  • @maxiroson
    @maxiroson 2 роки тому +5

    Nice video !!! Very clear explanation.

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому

      We're happy to know this lesson from Alejandra and Manuel provided some helpful guidance. If you are interested in learning more check out these awesome guides: developer.android.com/jetpack/getting-started

  • @amirhosseinghafoorian9985
    @amirhosseinghafoorian9985 2 роки тому

    It was an amazing workshop and nice presentation ale .

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

    Thanks guys!
    One question: which one should we use from these two?:
    - LiveData in the ViewModel with `val state by viewModel.liveData.observeAsState()` in the Composable?
    - or a MutableState like `var state by mutableStateOf(value)` directly in the ViewModel?

  • @aseemsharma4643
    @aseemsharma4643 2 роки тому +1

    Nice workshop! I am very eager to convert the existing codebase with composable functions, the only thing stopping is the backward compatibility, our app supports min SDK 16 and compose works from SDK 21 ☹

  • @AaronFleshner
    @AaronFleshner 7 місяців тому

    This was awesome thank you for this video is was great!

  • @alfredlotsu9580
    @alfredlotsu9580 2 роки тому +2

    Thanks for this

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

    Hi! The viewModel() documentation says that it: Returns an existing ViewModel or creates a new one in the given owner (usually, a fragment or an activity). So if my screens all live inside the same Activity would this mean that, after instantiating several screens, all those ViewModels are also instantiated and kept around while the Activity is still active? Is this efficient?

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

    Hi, great video.
    I have a question for the codelab in migrating check state.
    Why we need to change data class to regular class? How about like this? Thanks!
    data class WellnessTask(val id: Int, val label: String, var initialChecked: Boolean = false) {
    var checked by mutableStateOf(initialChecked)
    }

  • @harshpratapsingh1638
    @harshpratapsingh1638 2 роки тому +1

    Really it helped a lot clear my doubts about state hoisting

    • @AndroidDevelopers
      @AndroidDevelopers  2 роки тому

      Glad to hear you found the video helpful, Harsh 🙂
      You can also check out the following link to follow along with our Jetpack Compose codelab: goo.gle/3ssNnBO

  • @schinsky6833
    @schinsky6833 2 роки тому

    love the bright theme.

  • @BrunoMilhan
    @BrunoMilhan 2 роки тому +1

    Thanks guys :)

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

    How about if we change the screen from portrait to landscape with some checked tasks and then try to check new tasks with landscape screen if we go back to portrait mode why we lose the checked tasks after screen rotation?
    N.B: I used the rememberSabeable lambda also.

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

    awesome,,,,,, you guys taught us like baby, which is must must-needed

  • @KoradiyaAbhay
    @KoradiyaAbhay 2 роки тому +4

    Very great content. Want to see about every concept of compose.

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

      Jetpack Compose is like a sponge. There are so many wonderful features that you can absorb just like water! Want to learn more? Check here: goo.gle/3rBdmWO 🧽

  • @VoPoHo
    @VoPoHo 2 роки тому +2

    Great content, thanks!

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

      We have so many amazing resources for Jetpack Compose, and we can't wait to share them with you all! Check out our course here: goo.gle/compose-pathway
      All aboard the learning train 🚂

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

    helped me a lot, was struggling with state for a bit

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

      Thanks for watching! We're so happy to hear it was helpful 😎

  • @oraofficial7899
    @oraofficial7899 11 місяців тому

    well made video and good explanation video, thanks

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

    So usefull lesson! Thanks!

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

      Thank you so much for your feedback, Tom! We appreciate it! And of course you can also check out our State in Jetpack Compose Codelab for a more hands-on tutorial, here 😄 → goo.gle/3JVtzOP

  • @wilsonahanmisi1187
    @wilsonahanmisi1187 2 роки тому +2

    Nice One Ale and Manuel

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

    My big state-related question: What about remembering state across invocations of the application? Is there a clean way to do that?
    My reasoning:
    1. `rememberSaveable` will persist the state across screen rotations
    2. The way `rememberSaveable` works is to persist the data to a bundle
    3. Persisting to a bundle should also work across application restarts - but the framework does not do this for us
    I know I could go all the way to using a full database which I store the state in... but this is more complex than you might initially think!
    For example, if I have a call to `mutableStateOf()` in a function which is called via two separate paths, Compose will correctly create two separate state vars, and I don't have to think about it.
    So if I wanted to store state in a database, I'd have to have some way to generate a sensible key for the state. But implementing that myself requires somehow knowing Compose internals, and I'm not even sure how to do that reliably. Also consider what happens if a new version of the application comes out, where the path to the state changes. :(
    _Ideally_, `rememberSaveable` would just do this for us without having to write anything else. But it doesn't, so what should we be doing?

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

    niiice, saludos desde españa!

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

    Lets assume we are building a bank managemnt system , a user logs in , and the login details should be made available to all the screens in the app, when a user sends the app yo the background , he/she is logged out due to the sensitivity of the app ,how can we acheive this ?

  • @shehramjamil966
    @shehramjamil966 2 роки тому +1

    very informative.

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

      We're so glad to hear that you enjoyed it, Shehram! Be sure to check out the links in the description for even more tips and tutorials 😎

  • @user-not_defined
    @user-not_defined 9 місяців тому

    Couldn't you explain please. Why remember api doesn't working as rememberSaveable by default? Is it really so possible to score memory critically that it is necessary to separate? Maybe yes and good practice to separate api, a know it. But i'm interested)

  • @alfredlotsu9580
    @alfredlotsu9580 2 роки тому +2

    Man you guys are geniuses

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

    I wonder how Alejandra using these (step #) to past codes snippets so fast @30:14

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

      Great question and we're happy to share some insight!
      Live templates allow you to enter code snippets for fast insertion and completion of small chunks of code. They're great for saving time when you have a lot of repeating code you're needing to write ⏰
      You can learn more here: goo.gle/3MRH17L

  • @bolanoluwa6686
    @bolanoluwa6686 2 місяці тому

    Thanks

  • @johnhammer8668
    @johnhammer8668 11 місяців тому

    How is state implemented internally. Are there any talks or docs ?

  • @fernandogoncalves1891
    @fernandogoncalves1891 5 місяців тому

    where can we find all the steps? Thanks

  • @yasserakbbach7342
    @yasserakbbach7342 2 роки тому +3

    Just wow, can we have a workshop about Dagger Hilt with multi-module project?

    • @manuelvicnt
      @manuelvicnt 2 роки тому +1

      Thanks for the suggestion! We'll keep it in mind :) thanks!

    • @manuelvicnt
      @manuelvicnt 2 роки тому +1

      Thanks for the suggestion! We'll keep it in mind :) thanks!

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

      @@manuelvicnt make a tutorial on creating Menubar with jetpack compose those ones available on documentation are for XML

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

    Nice Vijeo

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

    val count: MutableState = mutableStateOf(0)
    Here, mutableStateOf(0) was showing error, that is why used direct
    val count = remeber { mutableStateOf(0) }
    then it worked correctly

  • @s4ril
    @s4ril 2 роки тому

    looks like for `BacisStateCodelab` project end solution was merged to the main branch ;(

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

      You start from a blank project and the code in main branch is the final/solution code.

  • @someperson6343
    @someperson6343 2 роки тому

    This is a great video, and much more informative and interesting than the code labs due to your recommendations, tips, key concepts etc. Also code labs are boring and you too are the opposite. Its fun to watch with a mega pint of coffee.

  • @leandroperalta
    @leandroperalta 2 роки тому

    Che, la versión en español cuando sale? 😉

  • @denisandreev2835
    @denisandreev2835 2 роки тому +1

    Thanks! Nice tutorial! But what if checkbox state was part of your dataclass (as selected: Boolean)? I can't figure out how to handle this situation.

    • @hdokit9248
      @hdokit9248 2 роки тому +2

      That is described in the last step of the Codelab,. Check it out: developer.android.com/codelabs/jetpack-compose-state#11

    • @denisandreev2835
      @denisandreev2835 2 роки тому

      @@hdokit9248 Thanks! 👍

  • @517Amit
    @517Amit Рік тому

    Have watched 🙂

  • @SYBCA
    @SYBCA 7 днів тому

    My screen is boosted 🎉

  • @cularu1
    @cularu1 2 роки тому +1

    flutter style stateful and stateless widgets?

    • @saidooubella
      @saidooubella 2 роки тому +1

      Kind of, it's just up to you to make them either stateless or stateful. In Flutter it's forced through inheriting the StatelessWidget or StatefulWidget IMO.

  • @nayandhabarde
    @nayandhabarde 2 роки тому +2

    Why should I not use rememberSaveable always instead of remember

    • @AlejandraStamato
      @AlejandraStamato 2 роки тому +4

      This is a great question!
      You should use rememberSaveable if that's what your UX requires, as it creates a persistent source of truth, it really does depend on your usecase. There might be cases where you might not want to restore the screen to what it used to be. Also, creating Savers for some objects might not be trivial and it becomes a bit boilerplate-y.

  • @aliisazadeh6973
    @aliisazadeh6973 2 роки тому +2

    please code along the workshop not just copy and paste the code

  • @ksenchy
    @ksenchy 2 роки тому +3

    No Pancho this time? 🤣 Great video for people starting with compose

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

    We are having a lot of problems syncronizing the dependencies ,can you help us come up with a way, just as in the spring boot initializer to create an app and include all dependecies without any conflicts ,thanks you so much .

  • @Shakesbeer1
    @Shakesbeer1 2 роки тому

    Whata is the reason to have 'remember' at all?

    • @AlejandraStamato
      @AlejandraStamato 2 роки тому +3

      For simpler state to be retained across recomposition, animation state is in general a good use case.

    • @Shakesbeer1
      @Shakesbeer1 2 роки тому

      @@AlejandraStamato makes sense

  • @KangJangkrik
    @KangJangkrik 2 роки тому

    I don't understand why assigning Modifier class as function parameter, it's not an object after all

    • @saidooubella
      @saidooubella 2 роки тому +2

      It is. It is Modifier's companion object that implements the Modifier interface.

    • @KangJangkrik
      @KangJangkrik 2 роки тому

      @@saidooubella I came from Java mindset, what I confused is why not use it directly from "import" keyword. Companion is equivalent as static in Java, right?

    • @saidooubella
      @saidooubella 2 роки тому +1

      @@KangJangkrik Yes. In Kotlin-JVM companion objects are just inner static singleton classes.

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

    Greet

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

    👋🏼🇨🇴🧔🏻👍🏻🤝🏻

  • @user-not_defined
    @user-not_defined 9 місяців тому

    Oh my eyes, white theme in Android Studio😅

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

    removing remember function to keep state would be more intuitive

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

    Great Codelab but the code-along in this one left alot to be desired. the cut and paste is not a good idea for code-along

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

    I am at wrong place😴😴😴

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

    In the documentation it mention that we will need to add this to the build.gradle "implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1" but is not working. You will get the following error message "Duplicate class kotlin.internal.jdk7.JDK7PlatformImplementations$ReflectSdkVersion found in modules kotlin-stdlib-1.8.10"

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

    What I have to do if i want to use the texfield value and want to change its state outside the function. I have done it by returning the String from the textfield that is initialized as the variable that stores the remember savable I have returned it but my app is crashing..🥲🥲