1. LiveData is an lifecycle aware obervable data holder ( means it knows the lifecycle of the activity or an fragment) use it when you play with UI elements(views). 2. StateFlow(hot stream) does similar things like LiveData but it is made using flow by kotlin guys and only difference compare to LiveData is its not lifecycle aware but this is also been solved using repeatOnLifecycle api's, So whatever LiveData can do StateFlow can do much better with power of flow's api. 3. SharedFlow(hot stream) - name itself says it is shared, this flow can be shared by multiple consumers, I mean if multiple collect calls happening on the sharedflow there will be a single flow which will get shared across all the consumers unlike normal flow. 4. Flow (cold stream) - In general think of it like a stream of data flowing in a pipe with both ends having a producer and consumer running on a coroutine.
Hi, thanks for the explanation. I have a question tho, you said that StateFlow can do anything that LiveData does but better, can you give me an example of what it is? Because I've been using LiveData on some of my apps but never find a problem where i need to use Flow/StateFlow/SharedFlow
@@myrmecii578 Most important thing with flows are native to kotlin and they are not platform dependent like livedata, so if have our codebase with flows they are pretty much useful when we use KMM in future and secondly I feel the flow is having pretty good list of operators which we can seriously playaround and with coroutines support. And flows can be used in different layers of the app altogether and can be glued together in much idiomatic style.
Great explanation. Also, a normal Flow (cold) can be converted to a hot flow using shareIn. For example val latestFood: Flow = flow { ... }.shareIn( externalScope, replay = 1, started = SharingStarted.WhileSubscribed() )
This video is brilliant. I decided to rebuild our company app using Kotlin and Jetpack Compose. You really helped me get my head around things. Thank you very much!
As someone who is new to reactive programming, this video is quite useful. LiveData, Flow, StateFlow, SharedFlow, Compose states, it all makes sense now. I'll check out another concept (Channel) and then proceed to one of your MVVM projects 😊
Big mistake here: Neither LiveData nor StateFlow keep the state alive on activity re-creation/rotation. That's the job of the ViewModel. I'm not trying to nitpick here, but you've mentioned it several times (2:20, 6:22) and it's definitely a mistake. The purpose is LiveData/StateFlow is to call you back when the value changes, instead of you repeatedly checking if changes happened. This is called the observer pattern, where LiveData/StateFlow is the observable (holding the value you are interested in) and your activity is the observer (someone who is interested in changes) Again, if you put LiveData/StateFlow into the activity or fragment instead of the ViewModel, then the state will be lost on screen-rotation.
Noop, you are made mistake. ViewModel just keep single instance of liveData when you rotate device. If you will create liveData in Fragment or Activity, it won't work as you expect because will be created new instance of liveData. Am i right?
@@MrEmailll correct, the ViewModel/ViewModelProvider are responsible for keeping a single instance alive over activity re-creations. LiveData doesn't do that by itself, LiveData is just a lifecycle aware observable, if you put it in a fragment or activity you will get a new instance on activity re-creation just like any other member of fragements/activities
6:59 the reason of preferring flow over livedata is not having more operators basically you can do that with live data too but in livedata that happens on main thread but in flows you can switch the context whereever you want.
Thanks for your great content ! I'm a professional Android developer but I've been working with RxJava and XML, and your videos have been a great summary of what I need to know about Compose and Coroutines !
I think it not completely correct. SharedFlow can be replay for many consumers whereas StateFlow won't. Actually you can emit the same value for the StateFlow, just need the other object address. The main different usage between SharedFlow vs StateFlow is: - SharedFlow is hold the Event - StateFlow is hold the Data
Wish you would've mentioned Channels in this video too, to send events, etc. I used Coroutines Channels in one of my projects and from what I understand they are similar to SharedFlow
SharedFlow as a hot flow will emit data even if no one listening. Channel will hold data till someone consumes it. So if your view not ready to receive an event and sharedFlow emits it, the event will be lost. So channels are better to send one time events. Obviously you can set reply count for shared flow, but then your event will be repeated .
Thanks for the very important topic you covered. Do you have any video where you showed how to use StateFlow or SharedFlow with Retrofit api call along with state handle using Kotlin sealed class? If not, please (if possible) make a video on CRUD by showing how to combine all those things using MVVM pattern.
@8:06 StateFlows are hot unlike normal flow in a way like they can live without any active consumer (bcz data is produced outside of the stream and then sent into the flow) but it's not like it will emit without calling collect on it !!
Sorry, which way is better to passing data (List of data) "retrofit -> repository -> viewmodel -> view(compose)"? I want to periodically call data by WorkManager. Thank You.
Excellent summary. As for many other people, you are a reference in Android programming and I would like to know your opinion. Do you think we should continue using XML or is it definitely better to use Jetpack compose in new projects? Best regards and thanks.
Sharedflow save the State in screen rotation? For example if i use it for the Hello World example, the TextView will have the "Hello World!" or the "Shared flow"? After screen rotation
Can you show how to make an indefinite snackbar: 1. Survive screen rotation 2. Show again, if action performed Use case: If we were to use a snackbar for an error when fetching data I can only get one or the other to work
11:01 would like to add something when tou rotate screen your activity is getting recreated so to normal flow it’s just new new subscriber and you have to request the data again.
Hi Philipp, thanks for your great content. Could you maybe make a video about the current state of using Google Maps with Jetpack and Compose? It seems that Google Maps are not really working well with Compose right now...
THANK YOU SO MUCH!!! PLEASE HELP I have created single activity by watching your Clean MVVM Notes video, it uses Compose navigation, Problem is there is a button on B screen to send email, so it needs to startActivity(context,intent), but I cannot get context, i tried all of these couldnt get it working, so is it possible that I can fire event from Compose screen B and startActivity is done by MainActivity? this context is making it so hard
Just come back to the Android after months working in the backend domain, these flow stuff is really confusing, i also heard that there are channels and callbackflow
11:02 Why are you using collectLatest as a terminal operator?, collect can do the same job. Maybe I misunderstood something about collect operator. If anyone know the difference? Please comment here
How do I safely collect a Flow from Custom View or ViewHolder class? What's the right lifecycle scope to handle it. Ex: using your example Flow to simulate a Timer.
@@PhilippLackner But for example PagingDataAdapter where the collection is immutable from outside (PagingData) is not possible if I handle it from the ViewModel (Simulate a timer with a Flow). The question would rather be; Should a Flow be mandatorily tied to the Lifecycle when it is collected?
Hi Philipp! I have a doubt, I would like to know if It's possible to reduce request to server(p.g firebase-firestore) when I use flowState. Now, I'm using livedata but I do many request...so with FlowState with 1 request I could to keep the fresh data? This case is for a Social App.
Hard to understand what emit implies. Example, emit ("Item 1"), means the sw continuously emits the string (thousands, millions of times, exact number depends on the machine, CPU)? the program collects one of those strings, although on the background the software keeps emitting "thousands" of strings. Can the continuous emission impact the performance of the app?
I started android dev about 2 months ago and i learnt a lot of technical stuff,but how can i improve my ui/ux skills,because everybody puts the accent on the technical part I would love your advice if that's possible because im really struggling with the design part,basically making an app look good and professional
Design is a completely separate skill You can look at other apps and try to copy their style It takes practice and time to learn design Usually most developers work with designers bc it's such a different skill set
1. LiveData is an lifecycle aware obervable data holder ( means it knows the lifecycle of the activity or an fragment) use it when you play with UI elements(views).
2. StateFlow(hot stream) does similar things like LiveData but it is made using flow by kotlin guys and only difference compare to LiveData is its not lifecycle aware but this is also been solved using repeatOnLifecycle api's, So whatever LiveData can do StateFlow can do much better with power of flow's api.
3. SharedFlow(hot stream) - name itself says it is shared, this flow can be shared by multiple consumers, I mean if multiple collect calls happening on the sharedflow there will be a single flow which will get shared across all the consumers unlike normal flow.
4. Flow (cold stream) - In general think of it like a stream of data flowing in a pipe with both ends having a producer and consumer running on a coroutine.
basically rewirte rxjava with a nicer api enable by kotlin
Hi, thanks for the explanation. I have a question tho, you said that StateFlow can do anything that LiveData does but better, can you give me an example of what it is? Because I've been using LiveData on some of my apps but never find a problem where i need to use Flow/StateFlow/SharedFlow
@@myrmecii578 Most important thing with flows are native to kotlin and they are not platform dependent like livedata, so if have our codebase with flows they are pretty much useful when we use KMM in future and secondly I feel the flow is having pretty good list of operators which we can seriously playaround and with coroutines support. And flows can be used in different layers of the app altogether and can be glued together in much idiomatic style.
Great explanation. Also, a normal Flow (cold) can be converted to a hot flow using shareIn. For example
val latestFood: Flow = flow {
...
}.shareIn(
externalScope,
replay = 1,
started = SharingStarted.WhileSubscribed()
)
So SharedFlow is similar to Channels then?
That's what i want recently. it's legendary.
you're a magician. thank you for everything you give the subscribers all the videos!!
I watched lots of RUclips videos and doc but i can't understand the different. But your video is more clearly explained..thanks for the video.
googled hours didn't found what I was looking for. 5 minutes into you video and I figured it out. Thanks mate! Love your Videos!
This video is brilliant.
I decided to rebuild our company app using Kotlin and Jetpack Compose. You really helped me get my head around things.
Thank you very much!
As someone who is new to reactive programming, this video is quite useful. LiveData, Flow, StateFlow, SharedFlow, Compose states, it all makes sense now. I'll check out another concept (Channel) and then proceed to one of your MVVM projects 😊
you are the best teacher in the world, i really hope that you continue recording video for you channel very often, i really thank to you. xD
The best yet simplest explaination. Covers the use cases and differences both! Thanks a ton Philipp.
Big mistake here: Neither LiveData nor StateFlow keep the state alive on activity re-creation/rotation. That's the job of the ViewModel.
I'm not trying to nitpick here, but you've mentioned it several times (2:20, 6:22) and it's definitely a mistake.
The purpose is LiveData/StateFlow is to call you back when the value changes, instead of you repeatedly checking if changes happened.
This is called the observer pattern, where LiveData/StateFlow is the observable (holding the value you are interested in) and your activity is the observer (someone who is interested in changes)
Again, if you put LiveData/StateFlow into the activity or fragment instead of the ViewModel, then the state will be lost on screen-rotation.
that's right.
Thanks! Where can I download the sample code in the videos?
Noop, you are made mistake. ViewModel just keep single instance of liveData when you rotate device. If you will create liveData in Fragment or Activity, it won't work as you expect because will be created new instance of liveData. Am i right?
@@MrEmailll correct, the ViewModel/ViewModelProvider are responsible for keeping a single instance alive over activity re-creations.
LiveData doesn't do that by itself, LiveData is just a lifecycle aware observable, if you put it in a fragment or activity you will get a new instance on activity re-creation just like any other member of fragements/activities
@@MrEmailll that's right .
You don’t know how much I needed this
6:59 the reason of preferring flow over livedata is not having more operators basically you can do that with live data too but in livedata that happens on main thread but in flows you can switch the context whereever you want.
Thanks a lot for this! I had surrounded myself with Android Jetpack libraries and wanted to check out Flow. I'll definitely try out more of SharedFlow
You deserve more subs.. one of the best android channel
This was a fantastic explanation and comparison. Thank you Philipp
Woho! I am glad you covered this topic!!! Thanks!!!
(came to know about these while working on a project)
Thanks for your great content ! I'm a professional Android developer but I've been working with RxJava and XML, and your videos have been a great summary of what I need to know about Compose and Coroutines !
One important feature of SharedFlow is that it will emit the same value, and StateFlow won't.
I think it not completely correct.
SharedFlow can be replay for many consumers whereas StateFlow won't.
Actually you can emit the same value for the StateFlow, just need the other object address.
The main different usage between SharedFlow vs StateFlow is:
- SharedFlow is hold the Event
- StateFlow is hold the Data
The video I was waiting for. Thank you 💕
Niiice man, I'm glad that I found this video only one day after I had this question myself.
Nice explanations dude! Your video has helped me a lot and thanks to everybody in the comments for errata.
You make real Topics man... keep making keep growing...😀😀
Really helped me a lot as your all videos do ❤️
Thx man and keep sharing your quality content more and more you are the best 👏
I love you......
I always have been curious of those differences.
Thank you.
Great Video Philipp, really explained a lot, thanks
Wish you would've mentioned Channels in this video too, to send events, etc.
I used Coroutines Channels in one of my projects and from what I understand they are similar to SharedFlow
Me too, and I'd like to know, which is better for this kind of 'event' operations
SharedFlow as a hot flow will emit data even if no one listening. Channel will hold data till someone consumes it. So if your view not ready to receive an event and sharedFlow emits it, the event will be lost. So channels are better to send one time events. Obviously you can set reply count for shared flow, but then your event will be repeated .
@@alexkoromyslov8537 Thank you sir 👍
Thanks for the very important topic you covered. Do you have any video where you showed how to use StateFlow or SharedFlow with Retrofit api call along with state handle using Kotlin sealed class?
If not, please (if possible) make a video on CRUD by showing how to combine all those things using MVVM pattern.
That's what I am looking for amazing explanation
You're genius man I'm coming from c# loving your videos
Why StateFlow needs to be called on `launchWhenStarted()` not on `launch()`?
@8:06 StateFlows are hot unlike normal flow in a way like they can live without any active consumer (bcz data is produced outside of the stream and then sent into the flow) but it's not like it will emit without calling collect on it !!
Hot streams will emit values even if no one collecting it by definition of hot flow.
Awesome content ❤️. Can't thank you enough for your hard work
You say don't use LiveData, but many of the modern Room tutorials and documentation still use LiveData for returning live data from a database.
Sorry, which way is better to passing data (List of data) "retrofit -> repository -> viewmodel -> view(compose)"? I want to periodically call data by WorkManager. Thank You.
Depends on the use case.
If its a single shot then normal data type
If its a short poll or web hook something that works with live data or flow.
Thanks Philipp well Explained.
Thanks for explaination in detail. I'll try it later :)
today i was looking for this topic
Nice video, thanks Philipp! It would help me more if you would describe real world examples of when to use what type of flow.
Thank you very much!
As usual, great video.
Good stuff!! Thank you!
Excellent summary. As for many other people, you are a reference in Android programming and I would like to know your opinion. Do you think we should continue using XML or is it definitely better to use Jetpack compose in new projects? Best regards and thanks.
In one of his videos he said that JetpackCompose is not suitable for complex projects, at least not yet. But I´m not sure if that´s still the case.
@@frantardencilla2908 Thanks mate, I missed that video
Thanks for great explanation. Nice Content 👏
Reactive programming, very good. Thanks 📈
Hello, i want to use a variable across activities , which video i should watch from your playlist, thank you !
Very good explanations.
Really really helpful. Thanks a lot
This was fantastic thank you
What's the difference between MutableStateFlow and MutableStateOf? Can you make a video for that? THANK YOU!
Sharedflow save the State in screen rotation? For example if i use it for the Hello World example, the TextView will have the "Hello World!" or the "Shared flow"? After screen rotation
This is awesome 👍
Hi, thank for this excelent video, but i have a question. how we can imitate the behaviour of "MediatorLiveData" with flow?
For the umpteenth time, you are amazing. Any plans soon of making videos for Espresso UI tests for jetpack compose?
bro i'am suggest you to create video about offline first / caching data from api to room db, thank you
Can you show how to make an indefinite snackbar:
1. Survive screen rotation
2. Show again, if action performed
Use case:
If we were to use a snackbar for an error when fetching data
I can only get one or the other to work
11:01 would like to add something when tou rotate screen your activity is getting recreated so to normal flow it’s just new new subscriber and you have to request the data again.
Hi Philipp, thanks for your great content. Could you maybe make a video about the current state of using Google Maps with Jetpack and Compose? It seems that Google Maps are not really working well with Compose right now...
THANK YOU
Thank you for this tutorial...
THANK YOU SO MUCH!!! PLEASE HELP I have created single activity by watching your Clean MVVM Notes video, it uses Compose navigation, Problem is there is a button on B screen to send email, so it needs to startActivity(context,intent), but I cannot get context, i tried all of these couldnt get it working, so is it possible that I can fire event from Compose screen B and startActivity is done by MainActivity? this context is making it so hard
You're a lifesaver
(Irrelevant question) What software do you use for the recoding?
Great info!
Please make a video on compose states
Thanks! Your video is great, where can I download the sample code in the video for testing?
For one time events such as error toasts, should I use SharedFlow or Kotlin Channels?
Just come back to the Android after months working in the backend domain, these flow stuff is really confusing, i also heard that there are channels and callbackflow
11:02 Why are you using collectLatest as a terminal operator?, collect can do the same job.
Maybe I misunderstood something about collect operator. If anyone know the difference? Please comment here
CollectLatest gets the most recent value emitted, and collect will take every value in the order it was emitted
🙌🏻🇨🇴🧔🏻👍🏼🤝🏼👉🏼🖥️📱 Saludos desde Colombia.
thanks
So, no need to create channels and convert to sharedflow.
hopefully you can do the same in jetpack compose because i still confusing how to observe single event by using Channel from viewmodel
谢谢!
How do I safely collect a Flow from Custom View or ViewHolder class? What's the right lifecycle scope to handle it. Ex: using your example Flow to simulate a Timer.
That's not the job of the view. It should be maintained in the viewmodel
@@PhilippLackner But for example PagingDataAdapter where the collection is immutable from outside (PagingData) is not possible if I handle it from the ViewModel (Simulate a timer with a Flow). The question would rather be; Should a Flow be mandatorily tied to the Lifecycle when it is collected?
You're the best!!
So what's the difference between channel and sharedflow?🤔
Thank you boy you are gr8
Need to point out that StateFlow is also null safety
Waiting video about local, remote date sources )
Great Video
7:35 hot flow vs cold flow
very nice education 👌❤❤
Is the source code available?
Can we replace callbacks with flows? Specifically sharedFlow?
Gread Videad !!!
😀😀
amazing video
Hi Philipp!
I have a doubt, I would like to know if It's possible to reduce request to server(p.g firebase-firestore) when I use flowState. Now, I'm using livedata but I do many request...so with FlowState with 1 request I could to keep the fresh data?
This case is for a Social App.
👏👏👏
What about Channels ?
Could anyone share on how can MediatarLiveData be substituted with SharedFlow?
Hard to understand what emit implies. Example, emit ("Item 1"), means the sw continuously emits the string (thousands, millions of times, exact number depends on the machine, CPU)? the program collects one of those strings, although on the background the software keeps emitting "thousands" of strings. Can the continuous emission impact the performance of the app?
It's important to know the difference between cold flows and hot flows
very helpful!!!!!!!!
I started android dev about 2 months ago and i learnt a lot of technical stuff,but how can i improve my ui/ux skills,because everybody puts the accent on the technical part
I would love your advice if that's possible because im really struggling with the design part,basically making an app look good and professional
Design is a completely separate skill
You can look at other apps and try to copy their style
It takes practice and time to learn design
Usually most developers work with designers bc it's such a different skill set
wooow more videos like this 👍
I’m unable to hear what I made???? HELP
I’m using Java for android apps I don’t know kotlin it’s too hard to learn
Giga helpfull : )
❤️
Благодарю за урок
Flow is cold not hot!
🔥t🔥o🔥p🔥