A small screen shows a summary from a ViewModel and also receives a one-time refresh event. After returning to the screen or after the window is recreated, part of the data should be restored, and a fresh response should arrive only when the screen is visible and active.
Requirements
Android API 35, Kotlin 2.0, kotlinx.coroutines 1.8.1 are used.
- The app runs in the
app process on a device or emulator with minSdk 26.
- The screen collects state from
StateFlow in the ViewModel through a lifecycle-aware approach.
- The one-time refresh event must not be lost during a normal configuration change, but it also must not fire again on every new
collect.
- The saved text must survive Activity recreation.
- Navigation is not used in the example; the event is modeled as
Flow<String>.
- It is assumed that the
ViewModel lives as long as the Activity, and the application process is not killed between reproduction steps.
Reproduction
- Open the summary screen.
- Press the refresh button.
- Minimize and reopen the screen through Activity recreation, for example by rotating the device.
- Wait for the UI to subscribe to the
ViewModel flows again.
- Repeat the refresh once more while the screen is in the background and then returns to the foreground.
Expected behavior
- After pressing the button, the summary changes to new text.
- If the Activity is recreated, the last text remains on the screen.
- The one-time refresh message is shown only when the screen is active.
- After returning to the screen, state and fresh events arrive without misses and without extra repeats.