Skip to content

Use collectAsStateWithLifecycle() for lifecycle-aware state collection - #160

Merged
ounben merged 30 commits into
ounben:masterfrom
Verlintas:fix/lifecycle-aware-collection
Nov 13, 2026
Merged

ounben merged 30 commits into
ounben:masterfrom
Verlintas:fix/lifecycle-aware-collection

Conversation

@Verlintas

@Verlintas Verlintas commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Replaces Compose's collectAsState() with collectAsStateWithLifecycle() in the UI and recorder composables (15 files, mechanical change).

Why

collectAsState() keeps collecting while the screen is in the background — e.g. when the user switches to another tab, opens the player from a list, or leaves the app. That keeps Room queries / view-model flows actively delivering updates and holding references longer than needed. collectAsStateWithLifecycle() pauses collection when the lifecycle drops below STARTED and resumes on return, which is the recommended pattern for Compose + StateFlow.

The project already depends on androidx.lifecycle:lifecycle-runtime-compose, so no build changes are needed.

Notes

  • Room Flow collectors use initialValue = ... (the lifecycle-aware overload's parameter name); plain StateFlow collectors are unchanged apart from the call.
  • No behavior change while a screen is visible.

AI disclosure

This PR was authored by an AI coding agent (OpenCode, running DeepSeek V4.1 Flash) under human supervision: the change, tests and description were generated by the agent and reviewed by the contributor before submission.

@ounben ounben self-assigned this Nov 12, 2026
@ounben ounben added the enhancement New feature or request label Nov 12, 2026
@ounben ounben added this to the November Release milestone Nov 12, 2026
@ounben

ounben commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Why collectAsStateWithLifecycle is better:

https://youtu.be/0G_1XCRlpYM?si=AXb_FFz7JZbD3zR1&t=466

https://developer.android.com/develop/ui/compose/state

collectAsState is similar to collectAsStateWithLifecycle, because it also collects values from a Flow and transforms it into Compose State.

Use collectAsState for platform-agnostic code instead of collectAsStateWithLifecycle, which is Android-only.

PR looks fine, I will double check again in android studio and merge it

@ounben
ounben self-requested a review November 13, 2026 16:21

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val recordState by recordManager.recordingState.collectAsStateWithLifecycle()

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben ounben left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateWithLifecycle(initialValue

@ounben
ounben merged commit 2a7c63f into ounben:master Nov 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants