Assigned
Status Update
Comments
il...@google.com <il...@google.com>
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-main
commit 50d3114e7f9713232b0483bc389eea0d09e268bb
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Sep 21 16:24:19 2021
Add lint rule for attaching and detaching same fragment
Since Fragment 1.3.0 and the switch over to the new state manager, the
FragmentManager now optimizes all operations within a single
transactions. This means that doing detach() and attach() in the same
transactions without using `reorderingAllowed(true)` will no longer
destroy and recreate the fragment view.
This lint rule detects usages of detach() and attach() in the same
transaction and warns developers that if they would like the old
behavior they should separate the operations into two different
transactions.
RelNote: "Fragments will now notify you via lint if you are attempting
to `detach()` and `attach()` the same fragment in a single
`FragmentTransaction` that it is a no-op and you should separate those
operations into separate transactions."
Test: added AttachAndDetachInSameTransactionTest
Bug: 173472486
Bug: 200867930
Change-Id: I917af67b88125d844d389f637c169583b38c453c
A fragment/fragment-lint/src/main/java/androidx/fragment/lint/AttachAndDetachInSameTransactionDetector.kt
A fragment/fragment-lint/src/test/java/androidx/fragment/lint/AttachAndDetachInSameTransactionDetectorTest.kt
M fragment/fragment-lint/src/test/java/androidx/fragment/lint/stubs/Stubs.kt
M fragment/fragment-lint/src/test/java/androidx/fragment/lint/ApiLintVersionsTest.kt
M fragment/fragment-lint/src/main/java/androidx/fragment/lint/FragmentIssueRegistry.kt
M fragment/fragment-lint/build.gradle
https://android-review.googlesource.com/1832956
Branch: androidx-main
commit 50d3114e7f9713232b0483bc389eea0d09e268bb
Author: Jeremy Woods <jbwoods@google.com>
Date: Tue Sep 21 16:24:19 2021
Add lint rule for attaching and detaching same fragment
Since Fragment 1.3.0 and the switch over to the new state manager, the
FragmentManager now optimizes all operations within a single
transactions. This means that doing detach() and attach() in the same
transactions without using `reorderingAllowed(true)` will no longer
destroy and recreate the fragment view.
This lint rule detects usages of detach() and attach() in the same
transaction and warns developers that if they would like the old
behavior they should separate the operations into two different
transactions.
RelNote: "Fragments will now notify you via lint if you are attempting
to `detach()` and `attach()` the same fragment in a single
`FragmentTransaction` that it is a no-op and you should separate those
operations into separate transactions."
Test: added AttachAndDetachInSameTransactionTest
Bug: 173472486
Bug: 200867930
Change-Id: I917af67b88125d844d389f637c169583b38c453c
A fragment/fragment-lint/src/main/java/androidx/fragment/lint/AttachAndDetachInSameTransactionDetector.kt
A fragment/fragment-lint/src/test/java/androidx/fragment/lint/AttachAndDetachInSameTransactionDetectorTest.kt
M fragment/fragment-lint/src/test/java/androidx/fragment/lint/stubs/Stubs.kt
M fragment/fragment-lint/src/test/java/androidx/fragment/lint/ApiLintVersionsTest.kt
M fragment/fragment-lint/src/main/java/androidx/fragment/lint/FragmentIssueRegistry.kt
M fragment/fragment-lint/build.gradle
Description
When using the new fragment state manager, all of the operations within a single FragmentTransaction are always optimized. This means that using complementary operations such as add/remove or attach/detach on the same fragment instance, in the same transaction will cancel each other out and end up not changing the state of the fragment.
For FragmentTransactions in the old world that were using
reorderingAllowed(true)
this will not change since that optimizes operations both within and between transactions. For all other transactions, if you would like to match the old world behavior you need to explicitly separate the complementary operations into two separate fragment transactions. As long as you use the samecommit()
method you were using before, the behavior should be identical.We should figure out a way to address this in the library. There should be a lint rule that warns against using complementary operations, in the same transaction, on the same fragment. It is less likely, but possible that we may also need to add some logic that detects these cases and runs them independently.