Fixed
Status Update
Comments
jb...@google.com <jb...@google.com>
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-main
commit 1e4222b878bd2a990871d75fa744995d09acedb9
Author: Jeremy Woods <jbwoods@google.com>
Date: Thu Jan 18 00:38:39 2024
Add lint rule for calling onBackPressed in callback
You should not call onBackPressed on the Activity or
OnBackPressedDisptacher inside of the handledOnBackPressed of an
OnBackPressedCallback. It is an anti-pattern and should be prevented.
This change adds a lint rule to ensure to call it out at compile time.
RelNote: "There is a now a lint rule to detect calls to onBackPressed
inside of OnBackPressedCallback.handledOnBackPressed."
Test: added test
Bug: 287505200
Change-Id: I3b5d8da34b9d8b43d765a05b8cc387aafc186df0
M activity/activity-lint/src/main/java/androidx/activity/lint/ActivityIssueRegistry.kt
A activity/activity-lint/src/main/java/androidx/activity/lint/OnBackPressedDetector.kt
A activity/activity-lint/src/test/java/androidx/activity/lint/OnBackPressedDispatcherTest.kt
M activity/activity-lint/src/test/java/androidx/activity/lint/stubs/Stubs.kt
https://android-review.googlesource.com/2915202
Branch: androidx-main
commit 1e4222b878bd2a990871d75fa744995d09acedb9
Author: Jeremy Woods <jbwoods@google.com>
Date: Thu Jan 18 00:38:39 2024
Add lint rule for calling onBackPressed in callback
You should not call onBackPressed on the Activity or
OnBackPressedDisptacher inside of the handledOnBackPressed of an
OnBackPressedCallback. It is an anti-pattern and should be prevented.
This change adds a lint rule to ensure to call it out at compile time.
RelNote: "There is a now a lint rule to detect calls to onBackPressed
inside of OnBackPressedCallback.handledOnBackPressed."
Test: added test
Bug: 287505200
Change-Id: I3b5d8da34b9d8b43d765a05b8cc387aafc186df0
M activity/activity-lint/src/main/java/androidx/activity/lint/ActivityIssueRegistry.kt
A activity/activity-lint/src/main/java/androidx/activity/lint/OnBackPressedDetector.kt
A activity/activity-lint/src/test/java/androidx/activity/lint/OnBackPressedDispatcherTest.kt
M activity/activity-lint/src/test/java/androidx/activity/lint/stubs/Stubs.kt
ap...@google.com <ap...@google.com> #3
Project: platform/frameworks/support
Branch: androidx-main
commit 2c950d0cfc22551a989265c4ffde9f69d5c69235
Author: Jeremy Woods <jbwoods@google.com>
Date: Wed Jan 24 22:21:05 2024
Add lint rule for calling onBackPressed in BackHandler
You should not call onBackPressed on the Activity or
OnBackPressedDispatcher inside of the onBack lambda of a BackHandler or
PredictiveBackHandler. It is an anti-pattern and should be prevented.
This change adds a lint rule to ensure to call this case out at compile
time.
RelNote: "There is now a lint rule to detect calls to `onBackPressed()`
inside of `BackHandler` and `PredictiveBackHandler` callbacks."
Test: added tests
Bug: 287505200
Change-Id: I94d12850523b5b56aa34a5355fdb2228a8ee1236
A activity/activity-compose-lint/src/main/java/androidx/activity/compose/lint/BackHandlerOnBackPressedDetector.kt
A activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/BackHandlerOnBackPressedDetectorTest.kt
M activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/CollectProgressDetectorTest.kt
A activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/Stubs.kt
https://android-review.googlesource.com/2926056
Branch: androidx-main
commit 2c950d0cfc22551a989265c4ffde9f69d5c69235
Author: Jeremy Woods <jbwoods@google.com>
Date: Wed Jan 24 22:21:05 2024
Add lint rule for calling onBackPressed in BackHandler
You should not call onBackPressed on the Activity or
OnBackPressedDispatcher inside of the onBack lambda of a BackHandler or
PredictiveBackHandler. It is an anti-pattern and should be prevented.
This change adds a lint rule to ensure to call this case out at compile
time.
RelNote: "There is now a lint rule to detect calls to `onBackPressed()`
inside of `BackHandler` and `PredictiveBackHandler` callbacks."
Test: added tests
Bug: 287505200
Change-Id: I94d12850523b5b56aa34a5355fdb2228a8ee1236
A activity/activity-compose-lint/src/main/java/androidx/activity/compose/lint/BackHandlerOnBackPressedDetector.kt
A activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/BackHandlerOnBackPressedDetectorTest.kt
M activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/CollectProgressDetectorTest.kt
A activity/activity-compose-lint/src/test/java/androidx/activity/compose/lint/Stubs.kt
jb...@google.com <jb...@google.com> #4
This has been fixed internally and will be available in the Activity 1.9.0-alpha03
release.
Description
Component used: Activity
Version used: 1.7.2
As explained in the Predictive Back guide , you shouldn't use an
OnBackPressedCallback
for non-UI cases (for example, just for logging back). If you add a callback, you have to handle back completely in your callback.That means that calling
remove()
and triggering back viarequireActivity.onBackPressed()
oronBackPressedDispatcher.onBackPressed()
is always an anti-pattern and something that you should not do.It would be helpful if there was a Lint error that specifically catch this case and warn developers ahead of time that this is the wrong thing to do and offer alternatives or at least link to the guide for more information.