Kotlin Flow API - Android Cheat Sheet 1.1 - Feb 2022
Kotlin Flow API - Android Cheat Sheet 1.1 - Feb 2022
RxJava
Type Supertype Description Features Implementation Code example Android usecase
equivalent
A specialized and limited • Always has an initial value • Can be configured with Similar to LiveData but with
version of SharedFlow that • Fixed replaysize of 1 sharing strategy from the far more operators and not
StateFlow • No buffering SharingStarted interface. Example A limited to mainthread. Rec-
SharedFlow requires an initial value and BehaviorSubject
emits a read only single • Read access to current • Eagerly, Lazily and While- ommended for KMM proj-
Example B
data value to its subscribers. value without collecting Subscribed (Always emits something) ects. Does require manual
lifecycle management.
Creates a Flow and allows • Conceptually very similar • Useful when you need to Converting multi shot An-
values to be emitted from a to a blocking queue run computations
Cold (emitsinlazy)
differ- Example A droid (Java) callback API’s
callbackFlow - different CoroutineContext. • Default capacity of 64 ele- - to a Flow like the onLoca-
ent CoroutineContexts Example B
Uses a hot SendChannel ments • Can convert cluncky call- tionResult() and the onTex-
(function, returns Flow) internally. • Buffer can be configured tChanged() listeners.
back API’s to nicer Flows