Questions tagged [kotlin-sharedflow]
52 questions
16
votes
2 answers
Use of SharedFlow in Android kotlin
Hey I am learning flow in kotlin. I am learning about MutableStateFlow and MutableSharedFlow. I tried to learn MutableStateFlow in real world example. But I am unable to get the MutableSharedFlow example, which place it suits more. I tried some…

Kotlin Learner
- 3,995
- 6
- 47
- 127
12
votes
1 answer
MutableSharedFlow - difference between replay and extraBufferCapacity
MutableSharedFlow takes 3 parameters: replay, extraBufferCapacity and onBufferOverflow. What is the difference between replay and extraBufferCapacity?
The documentation mentions the following:
replay - the number of values replayed to new…

Christian C
- 369
- 3
- 10
12
votes
2 answers
channel or mutablesharedflow , which one is a better replacement for deprecated localbroadcastmanager
In the past, I was using LocalBroadcastManager and EventBus in my chat and taxicab applications which are now either deprecated or not recommended using them.
I intend to replace them with new data structures like mutablesharedflow or channel,
i…

sok sok
- 215
- 4
- 9
9
votes
1 answer
how to avoid repeatOnLifecycle excute again and again when fragment resume
how can I avoid the collect{} code execute again when navigate back to the fragment.
ViewModel class
private val _commitResult = MutableStateFlow

Freddy
- 764
- 2
- 6
- 21
8
votes
5 answers
StateFlow last value is collected again in ui
So lately I've been working with StateFlow, SharedFlow, and Channels API's but I'm struggling with one common use case while trying to migrate my code from LiveData to StateFlow in the presentation layer.
The problem I'm facing is when I emit my…

Elias Bellido
- 81
- 2
- 8
6
votes
3 answers
SharedFlow is not collecting from emission
In my ViewModel, I am making API requests and I am using StateFlow and SharedFlow to communicate with the Fragment. While making the API request, I am easily able to update the state flow's value and it is successfully collected in the Fragment.
But…

Nishant Jalan
- 844
- 9
- 20
5
votes
0 answers
Clearing SharedFlow: Converting flow to mutableFlow
I've an API that returns flow which I'm converting to sharedFlow using shareIn(). I've a use case where I want to reset the cached value in the sharedFlow and force the flow to fetch new data. We've a resetReplayCache() however it's only available…

user2498079
- 2,872
- 8
- 32
- 60
5
votes
2 answers
LiveData Transformation to StateFlow / SharedFlow
What is the equivalent code for this live data transformation in StateFlow / SharedFlow?
val myLiveData: LiveData = Transformations
.switchMap(_query) {
if (it == null) {
…

Axel
- 685
- 5
- 14
4
votes
1 answer
Run a kotlin flow once but have it received twice downstream
Scenario
I have a hot flow EventHandler.sharedFlow emitted on a button click.
The flow is received by Repository that performs some action in OnEach{}.
The repository flow is then received by two event collectors EventCollectorA and…

mars8
- 770
- 1
- 11
- 25
4
votes
3 answers
SharedFlow in Android project not working as expected
I was trying to pass events from UI to viewModel using sharedFlow
this is my viewmodel class
class MainActivityViewModel () : ViewModel() {
val actions = MutableSharedFlow()
private val _state = MutableStateFlow(State.Idle)
…

noushad_chullian
- 133
- 11
3
votes
1 answer
Flow.stateIn() not receiving new value from it's source
I tried to combine several SharedFlow into one StateFlow using stateIn. But my StateFlow seems not updating after I emit new value to it's SharedFlow source. I found out that the problem is from how I used stateIn.
Here is the simplified code I am…

Jun
- 33
- 3
3
votes
1 answer
Android Fragment Collect SharedFlow from Shared ViewModel Issue
Currently I have a ViewPager2 Fragment (as the start destination), which holds two child Fragments: ActiveOrderFragment and CompletedOrderFragment, they both have their own ViewModels to handle the api calls (to get the active orders and completed…

Sam Chen
- 7,597
- 2
- 40
- 73
3
votes
1 answer
Flow emits different values when collecting it multiple times
I created a Flow from which I emit data. When I collect this flow twice, there are 2 different sets of data emitted from the same variable instead of emitting the same values to both collectors.
I have a simple Flow that I created myself. The text…

Christian C
- 369
- 3
- 10
3
votes
1 answer
When should i collect SharedFlow events in Fragment lifecycle
I have thought that is okay to collect SharedFlow data on onViewCreated. But when i replace fragment n times then fire some event to SharedFlow, it emits n times event to me instead of one event.
I have fixed it how i put my code on onCreate at my…

Gökberk Yağcı
- 376
- 1
- 4
- 10
3
votes
1 answer
Kotlin SharedFlow combine operation. Have zip behaviour in a specific situation
I'm combining two SharedFlows and then performing a long working operation.
At the start, I know the state so I emit a "starting value" for both the flows. After that user can emit to either flows.
Both flows are mostly independent but in a specific…

Favolas
- 6,963
- 29
- 75
- 127