-
Notifications
You must be signed in to change notification settings - Fork 7
Add retained chip text field state #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: dokar3/ChipTextField
Length of output: 6070
🌐 Web query:
androidx.compose.runtime.retain retain(key) key lifetime documentation short-lived reference Context View leak💡 Result:
The retain API, introduced in androidx.compose.runtime:runtime-retain [1][2], is a mechanism for state memoization that provides a lifespan longer than remember but shorter than persistence solutions like rememberSaveable [1]. While remember is scoped to the immediate composition hierarchy, retained values are stored in a RetainedValuesStore and can persist through transient destruction and recreation (e.g., configuration changes) [3][1]. Crucially, retained values persist longer than the lifespan of the specific composable that created them [3]. This extended lifetime introduces significant risks of memory leaks if not managed correctly [3]. Memory Leak Prevention Guidelines: 1. Avoid Retaining Context and Views: Never retain an Android Context or any object that references a Context, either directly or indirectly [3]. This includes Views, as they hold references to the Context that created them [3]. A rule of thumb is that if you would not store an object in a ViewModel, you should not retain it using the retain API [1]. 2. Key Lifetime: The keys provided to retain are referenced as long as the value is retained [1]. Ensure that these keys themselves do not inadvertently hold onto long-lived references that could prevent garbage collection. 3. Use Annotations: To prevent accidental retention of unsafe types, you can annotate your class definition with androidx.compose.runtime.annotation.DoNotRetain [3]. 4. Resource Management: If your retained object manages resources, implement RetainObserver to receive callbacks like onRetained and onRetired [4][5]. Always release resources or cancel jobs in onRetired to prevent leaks [4][6]. 5. Explicit Disposal: When using utilities like RetainedValuesStoreRegistry, ensure that you call dispose when the registry is no longer needed [6]. Failure to do so may result in leaked memory from undispatched RetainObserver callbacks [6]. In summary, retain is a powerful tool for maintaining state across configuration changes without serialization, but it effectively acts as a cache that lives as long as the app process (or the associated RetainedValuesStore). Treat it with the same memory-safety caution as you would a ViewModel [1].
Citations:
Document the retained key lifetime constraint.
retainChipTextFieldState(..., key)keeps the key alive as long as the retained value, so avoid passing short-lived references likeContextorView-backed objects.🤖 Prompt for AI Agents