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.
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.
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
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!
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
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
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
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 🧽
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
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?
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
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
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 🚂
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?
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 ☹
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
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.
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 ?
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.
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.
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)
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?
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) }
@@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?
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.
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 .
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"
8:34 its not a religion to believe ! add in Text() ", color= colorResource(getColorR()" and a function outside the scope: private fun getColorR():Int{ val color:Int = Random.nextInt(4) var result_:Int=0 when (color) { 0 -> result_=R.color.teal_700 1 -> result_=R.color.teal_200 2 -> result_=R.color.purple_200 3 -> result_=R.color.purple_500 } return result_ } And you gonna see how Text redraws itself by button click
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..🥲🥲
Wow! Thanks for this presentation, the speakers did an incredible job together. Excited to watch new content with you two folks
Hey, Jonathan! We think so too! It's always a pleasure to see how excited viewers are for more content 🙂
@@AndroidDevelopers p☺️
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.
I truly didn't know how to thank you for giving this to us for free.
I like that Manuel’s intro was stateful 😂
🤣🤣🤣
It's really just the State of mind. 😎
🤣🤣🤣🤣
@@AndroidDevelopers😢
@@AndroidDevelopers😢
Ale you are the best! Thank you so much for the presentation.
Great workshop! I really like the clear explanation and solved my doubts about the Composable States. You rock guys!
Thanks for the wonderful feedback; we're glad it was helpful! 😁
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.
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
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!
Great workshop!. Thanks Alejandra and Manuel 🥳
We're delighted that you've enjoyed the lesson, Yair. Thanks for the support!
Amazing presentation! What a star Ale! Thank you.
Hi António! We couldn't agree more. Thanks for watching✨
Great workshop. Most of my concepts are cleared now. Thanks to both.
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
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
Thanks for the presentation, amazing duo.
Great video! Congrats Alejandra and Manuel! Looking forward for more explanations like this :]
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
Yes I did code along. This video solved my big problem of not maintaining state of switch. Thanks a lot.
We're happy to hear this, Dhananjay! If you'd like to learn more about Jetpack Compose, check the following codelab: goo.gle/3ssNnBO 😀
Very great content. Want to see about every concept of compose.
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 🧽
Nice video !!! Very clear explanation.
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
helped me a lot, was struggling with state for a bit
Thanks for watching! We're so happy to hear it was helpful 😎
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?
It was an amazing workshop and nice presentation ale .
nice video, it is helpful to understand the state in compose
Really it helped a lot clear my doubts about state hoisting
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
Nice One Ale and Manuel
If you are attempting to explain state while dodging the question of View Models or related architecture, you have succeeded.
Thanks for the great workshop.
love the bright theme.
awesome,,,,,, you guys taught us like baby, which is must must-needed
Just wow, can we have a workshop about Dagger Hilt with multi-module project?
Thanks for the suggestion! We'll keep it in mind :) thanks!
Thanks for the suggestion! We'll keep it in mind :) thanks!
@@manuelvicnt make a tutorial on creating Menubar with jetpack compose those ones available on documentation are for XML
I wonder how Alejandra using these (step #) to past codes snippets so fast @30:14
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
Man you guys are geniuses
This was awesome thank you for this video is was great!
Great content, thanks!
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 🚂
Thanks guys :)
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?
well made video and good explanation video, thanks
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 ☹
Thanks for this
So usefull lesson! Thanks!
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
niiice, saludos desde españa!
No Pancho this time? 🤣 Great video for people starting with compose
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.
Great job
My screen is boosted 🎉
Oh my eyes, white theme in Android Studio😅
very informative.
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 😎
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 ?
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.
thank you.
Why should I not use rememberSaveable always instead of remember
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.
where can we find all the steps? Thanks
How is state implemented internally. Are there any talks or docs ?
Che, la versión en español cuando sale? 😉
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)
Thanks
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?
Super cool!!!! Excellent tutorial
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.
That is described in the last step of the Codelab,. Check it out: developer.android.com/codelabs/jetpack-compose-state#11
@@hdokit9248 Thanks! 👍
please code along the workshop not just copy and paste the code
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)
}
Nice Vijeo
Have watched 🙂
looks like for `BacisStateCodelab` project end solution was merged to the main branch ;(
You start from a blank project and the code in main branch is the final/solution code.
I don't understand why assigning Modifier class as function parameter, it's not an object after all
It is. It is Modifier's companion object that implements the Modifier interface.
@@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?
@@KangJangkrik Yes. In Kotlin-JVM companion objects are just inner static singleton classes.
flutter style stateful and stateless widgets?
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.
removing remember function to keep state would be more intuitive
Whata is the reason to have 'remember' at all?
For simpler state to be retained across recomposition, animation state is in general a good use case.
@@AlejandraStamato makes sense
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 .
Greet
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
👋🏼🇨🇴🧔🏻👍🏻🤝🏻
I am at wrong place😴😴😴
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"
8:34 its not a religion to believe !
add in Text() ", color= colorResource(getColorR()"
and a function outside the scope:
private fun getColorR():Int{
val color:Int = Random.nextInt(4)
var result_:Int=0
when (color) {
0 -> result_=R.color.teal_700
1 -> result_=R.color.teal_200
2 -> result_=R.color.purple_200
3 -> result_=R.color.purple_500
}
return result_
}
And you gonna see how Text redraws itself by button click
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..🥲🥲