Update getCurrentActivity in GoogleAuthModule.kt#15
Update getCurrentActivity in GoogleAuthModule.kt#15sbaiahmed1 merged 1 commit intosbaiahmed1:mainfrom
Conversation
📝 WalkthroughWalkthroughReplaced direct Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
android/src/main/java/com/googleauth/GoogleAuthModule.kt (2)
303-307: Consider usinggetValidActivity()for consistent activity validation.The
getTokensmethod checks only for null activity, whilesignIn(line 189) uses thegetValidActivity()helper that also validates the activity isn't finishing or destroyed. UsinggetValidActivity()consistently would prevent potential issues if the activity state changes between the check and usage inperformSilentSignIn.♻️ Proposed refactor for consistent validation
- val activity = reactApplicationContext.currentActivity - if (activity == null) { - promise.reject("NO_ACTIVITY", "No current activity available") + val activity = getValidActivity() + if (activity == null) { + promise.reject("NO_ACTIVITY", "No valid activity available. Please ensure the app is in the foreground.") return }
350-354: Consider usinggetValidActivity()for consistent activity validation.Similar to
getTokens, this method only checks for null activity. Using thegetValidActivity()helper would provide consistent validation across all methods that require an activity reference.♻️ Proposed refactor for consistent validation
- val activity = reactApplicationContext.currentActivity - if (activity == null) { - promise.reject("NO_ACTIVITY", "No current activity available") + val activity = getValidActivity() + if (activity == null) { + promise.reject("NO_ACTIVITY", "No valid activity available. Please ensure the app is in the foreground.") return }
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
android/src/main/java/com/googleauth/GoogleAuthModule.kt
🔇 Additional comments (2)
android/src/main/java/com/googleauth/GoogleAuthModule.kt (2)
441-445: LGTM! Proper use of safe call operator.The safe call with
letis the correct Kotlin idiom here, gracefully handling the case where the activity might be null when attempting to show the error dialog.
456-464: LGTM. ThegetValidActivity()helper correctly validates activity state before use, checking that it's not null, finishing, or destroyed. This defensive pattern prevents runtime crashes from stale activity references.
|
@sbaiahmed1 Ready to go! |
|
Hello @channel101 , Thank you for your contribution :D |
Thanks! |
This document explains the full evolution of how the current
Activityis accessed in React Native Android native modules, and why this change was required for modern React Native versions.1. Legacy Behavior:
currentActivity(Pre–RN 0.60)In older React Native versions, native modules (Java-based) could directly access the current Activity via an implicitly exposed field:
currentActivityWhen these modules were later converted to Kotlin, some codebases continued to rely on Kotlin’s lenient property access, treating:
as a valid reference, even though no explicit property existed.
This worked due to:
2. Official API:
getCurrentActivity()(RN 0.60 – RN 0.79)React Native standardized Activity access through
ReactContextBaseJavaModule:Correct usage required invoking the function explicitly:
However, many libraries incorrectly used:
This compiled in older toolchains but was technically invalid Kotlin.
3. Breakage with Modern Toolchains (Kotlin 1.9+ / AGP 8+)
With newer versions of:
The compiler became strict and no longer allowed treating function references as properties.
This resulted in build errors such as:
4. Deprecation in React Native 0.80
As of React Native 0.80,
getCurrentActivity()itself is deprecated:The recommended and future-proof API is now:
This property:
Activity?)5. Final, Recommended Implementation
This approach:
Summary Table
currentActivitygetCurrentActivity()reactApplicationContext.currentActivityConclusion
This change sequence explains why older libraries break on modern React Native setups and why updating to
reactApplicationContext.currentActivityis required to maintain compatibility going forward.Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.