Is it possible to update Kotlin StateFlow without emitting the change? The use case is that when user zooms a chart view, I would like to have that restored when activity resumes but skip the StateFlow events while zooming is in progress.
CodePudding user response:
Yes. Although it may lead to obscure bugs.
On a StateFlow<T> you can get the latest known value (bear in mind that it might not exist). Since Kotlin (like java) uses references for objects (it might not work for primitive types) then you may apply modifications to returned object from yourState.value.
CodePudding user response:
If you want to restore the latest state only when your Activity resumes, then don't collect the flow. You can get the latest value from the StateFlow.value property in Activity.onResume(). But then again, then you don't need a Flow in the first place and can simply use a property that directly stores the value you want.
