StateFlow vs SharedFlow in Kotlin: Interview Guide
Preparing for an Android interview? You probably don't need another 2,000-word lecture on Flow. You need to remember one thing quickly: StateFlow → What is the current state? SharedFlow → What happened? That simple question will help you choose between them in many interview situations. StateFlow vs SharedFlow — Quick Revision StateFlow SharedFlow Represents current state Broadcasts values/events Always has a current value Does not require an initial value New collector gets the latest value Replay is configurable Always keeps one latest value Can replay multiple previous values Great for UI state Useful for events and broadcasts Memory trick: StateFlow = State SharedFlow = Share Don't memorize the table. Ask yourself: "Does my UI need to know the current condition, or do I need to tell collectors that something happened?" 1. What is StateFlow? StateFlow is a hot flow that always holds a current value. A new...