Fixed
Status Update
Comments
mv...@google.com <mv...@google.com> #2
A couple of questions:
1. Have you saw crash in real device or only in simulators?
2. Do you use dynamic feature for language ID?
1. Have you saw crash in real device or only in simulators?
2. Do you use dynamic feature for language ID?
il...@google.com <il...@google.com> #3
Tested on Android 12 Emulator with custom executor, but cannot repro this issue.
Description
Background
The current Android architecture guidance recommends collecting flows in the UI in a lifecycle-aware manner. This allows the app to save expensive resources when not needed, for example, when the app is in the background.
Keeping expensive resources (e.g. Firabase queries, location or network updates, etc.) alive when not needed could impact the performance of the app and user's device.
To make it easier for developers to comply with the recommended best practices, APIs that do this should be added to the Lifecycle library.
Limitations with StateFlow
Ideally, there should exist APIs for both
Flow
andStateFlow
types. The latter is important since most flows consumed in Compose code will be of that type. If a helper function forStateFlow
doesn't exist, the UI (Compose code) would need to give those new functions an initial value (same as with Compose'sproduceState
or Flow'sstateIn
operator). That violates layering responsibilities since deciding the initial value belongs to the ViewModel/state holder, not the UI.However, as stated in the refdocs , the
StateFlow
interface is not stable for inheritance in 3rd party libraries, which makes it a tough call to create extensions functions on the type even though only the currentvalue
would be used.Proposed implementation #1
The
collectAsStateWithLifecycle
composable function is added to a newlifecycle-runtime-compose
artifact.Typically, this
collectAsStateWithLifecycle
API would be used like:Proposed implementation #2
To avoid passing the
initialValue
every time forStateFlow
types, another alternative could be therememberStateWithLifecycle
API. This API would also be added to a newlifecycle-runtime-compose
artifact.This would be used as follows:
This API would also offer a method overload that can take a
Flow
and aninitialValue
as parameters.