Skip to content

chore: Sample Code for implementing a onBackPressed detector#517

Open
kingargyle wants to merge 1 commit intodevelopfrom
dac_example_onBackPressedDetector
Open

chore: Sample Code for implementing a onBackPressed detector#517
kingargyle wants to merge 1 commit intodevelopfrom
dac_example_onBackPressedDetector

Conversation

@kingargyle
Copy link
Contributor

@kingargyle kingargyle commented Feb 10, 2026

Summary by CodeRabbit

  • New Features
    • Added a new lint rule that detects when developers override the deprecated onBackPressed() method and recommends migrating to OnBackPressedDispatcher to support predictive back gestures.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 10, 2026

Walkthrough

The pull request introduces a new Android Lint detector PredictiveBackDetector that identifies deprecated onBackPressed() method overrides and recommends migrating to OnBackPressedDispatcher. The detector is registered in the lint issue registry alongside existing rules, with comprehensive test coverage validating detection across Kotlin and Java implementations.

Changes

Cohort / File(s) Summary
Predictive Back Lint Detector
lint-rules/src/main/java/us/nineworlds/serenity/lint/PredictiveBackDetector.kt
New Kotlin detector class implementing UAST scanning for onBackPressed() method overrides, reports deprecation warnings with migration guidance to OnBackPressedDispatcher, categorized as CORRECTNESS priority 5.
Lint Registry Update
lint-rules/src/main/java/us/nineworlds/serenity/lint/PreferenceMigrationIssueRegistry.kt
Registry updated to expose PredictiveBackDetector.ISSUE alongside existing LegacyPreferenceDetector.ISSUE in the issues list.
Detector Test Suite
lint-rules/src/test/java/us/nineworlds/serenity/lint/PredictiveBackDetectorTest.kt
New test class with three test methods validating detection behavior: Kotlin override detection, Java override detection, and non-override filtering, ensuring single warning without errors for each scenario.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 The linter hops forward, catching old code paths,
Deprecated onBackPressed meets its aftermath,
PredictiveBack whispers: use Dispatcher instead!
Rules guide developers where they should tread. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title mentions implementing an onBackPressed detector which directly aligns with the main changes: adding PredictiveBackDetector, registering it, and adding comprehensive tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dac_example_onBackPressedDetector

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
lint-rules/src/main/java/us/nineworlds/serenity/lint/PreferenceMigrationIssueRegistry.kt (1)

10-13: Registry name no longer reflects its scope.

The class is named PreferenceMigrationIssueRegistry but now also registers PredictiveBackDetector.ISSUE, which is unrelated to preference migration. Consider renaming to something more general (e.g., SerenityIssueRegistry) to avoid confusion as more detectors are added.

lint-rules/src/main/java/us/nineworlds/serenity/lint/PredictiveBackDetector.kt (1)

16-16: Minor: import UElement instead of using the fully-qualified name.

UMethod is already imported from org.jetbrains.uast, but UElement is referenced by its FQN. Adding the import would be more consistent.

Proposed fix

Add to the import block:

import org.jetbrains.uast.UElement

Then simplify:

-    override fun getApplicableUastTypes(): List<Class<out org.jetbrains.uast.UElement>> = listOf(UMethod::class.java)
+    override fun getApplicableUastTypes(): List<Class<out UElement>> = listOf(UMethod::class.java)
lint-rules/src/test/java/us/nineworlds/serenity/lint/PredictiveBackDetectorTest.kt (1)

83-102: Solid negative test case.

Correctly omits the activityStub since TestClass doesn't extend Activity, ensuring isOverride returns false. This validates that methods merely named onBackPressed aren't false-positived.

Consider also adding a test for a class that extends Activity but does not override onBackPressed() (e.g., overrides onCreate only) to confirm zero warnings in that scenario as well.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant