Hey Android devs!
Did you know that 70% of interviewers ask about StateFlow vs. SharedFlow? Make sure you're prepared!
Both SharedFlow and StateFlow are built upon the concept of a "hot" flow.
Unlike "cold" flows, which generate data for each collector, "hot" flows share emitted values among multiple collectors.
---
StateFlow:
- holder emitting its current state to new collectors
- has an initial value
- emits subsequent updates to that value
You can think of it as a LiveData replacement in the Kotlin Flow world.
Only the latest value is stored, and that value is emitted to new collectors.
---
SharedFlow:
- initial value is not mandatory
- can replay a certain number of previous emissions to new collectors
- supports features like buffering and backpressure
It allows more advanced use-cases, like broadcasting the same value to multiple collectors.
---
Important:
StateFlow is a specialized kind of SharedFlow.
You can think of StateFlow as a SharedFlow with a replay of 1 (always replays the latest value to new collectors).
This doesn't mean StateFlow is a direct subtype of SharedFlow in terms of OOP inheritance; it's more conceptual.
In a typical Android use-case:
👉 Use StateFlow when you have stateful data that you want to observe. It's analogous to observing a LiveData.
👉 Use SharedFlow when you need more advanced features like buffering, replaying, or when you have events that aren't tied to a particular state (like one-time events).
---
Android tech interview is a lot to handle. Make sure to come prepared.
Grab your copy of Android Interview Pro ebook here:
https://t.co/oyztDeynzg
@sockeqwe Either use the network profiler which is exists within Android Studio. Or use Chucker (https://t.co/0SXQzVNQSI)
Much easier to analyze requests/responses than in the network profiler.
Today we are officially launching Shortwave 🥳 a brand new experience for your Gmail account. Shortwave helps you email 💌 smarter and faster, so you can not only be more productive, but actually enjoy your email!
Sign up and get started for free: https://t.co/MX19lQmMGa
@FMuntenescu Do you also take interns? I'm currently doing my CS Bachelor in Munich and searching for an internship in the USA. I've been doing Android for almost 4 years now. ☺️
@Zhuinden@codinginflow This. `AppCompatActivity.onActivityResult()` was quite easy to use and straight forward. Recently I wanted to introduce my dad to android development and show him how to pass data between activites. Those contracts were way to complicated to understand for a newbie.
💁♂️ Programming tip: Make some time to learn a new data structure or algorithm every now and then. Implement it by yourself to verify that you've understood it properly.
Growing your repertoire is a fantastic investment in yourself. 🤓👍