Skip to content

Update getCurrentActivity in GoogleAuthModule.kt#15

Merged
sbaiahmed1 merged 1 commit intosbaiahmed1:mainfrom
channel101:fix/get_activity
Jan 12, 2026
Merged

Update getCurrentActivity in GoogleAuthModule.kt#15
sbaiahmed1 merged 1 commit intosbaiahmed1:mainfrom
channel101:fix/get_activity

Conversation

@channel101
Copy link
Copy Markdown
Contributor

@channel101 channel101 commented Jan 10, 2026

This document explains the full evolution of how the current Activity is 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:

currentActivity

When these modules were later converted to Kotlin, some codebases continued to rely on Kotlin’s lenient property access, treating:

currentActivity

as a valid reference, even though no explicit property existed.

This worked due to:

  • Java interoperability
  • Older Kotlin compiler permissiveness
  • Older Android Gradle Plugin behavior

2. Official API: getCurrentActivity() (RN 0.60 – RN 0.79)

React Native standardized Activity access through ReactContextBaseJavaModule:

fun getCurrentActivity(): Activity?

Correct usage required invoking the function explicitly:

val activity = getCurrentActivity()

However, many libraries incorrectly used:

val activity = getCurrentActivity

This compiled in older toolchains but was technically invalid Kotlin.


3. Breakage with Modern Toolchains (Kotlin 1.9+ / AGP 8+)

With newer versions of:

  • Kotlin (1.9 / 2.x)
  • Android Gradle Plugin (8.x)

The compiler became strict and no longer allowed treating function references as properties.

This resulted in build errors such as:

Function invocation 'getCurrentActivity()' expected.

4. Deprecation in React Native 0.80

As of React Native 0.80, getCurrentActivity() itself is deprecated:

'fun getCurrentActivity(): Activity?' is deprecated.
Use reactApplicationContext.currentActivity instead.

The recommended and future-proof API is now:

reactApplicationContext.currentActivity

This property:

  • Is nullable (Activity?)
  • Is lifecycle-aware
  • Works with the New Architecture (TurboModules)

5. Final, Recommended Implementation

val activity = reactApplicationContext.currentActivity
    ?: throw IllegalStateException("Current activity is null")

This approach:

  • Avoids deprecated APIs
  • Compiles on modern Kotlin/AGP
  • Is compatible with React Native 0.80+
  • Does not change runtime behavior

Summary Table

Era API Status
Legacy RN currentActivity ❌ Removed
RN 0.60–0.79 getCurrentActivity() ⚠️ Deprecated
RN 0.80+ reactApplicationContext.currentActivity ✅ Recommended

Conclusion

This change sequence explains why older libraries break on modern React Native setups and why updating to reactApplicationContext.currentActivity is required to maintain compatibility going forward.

Summary by CodeRabbit

  • Refactor
    • Improved internal code organization for activity reference handling in authentication flows.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 10, 2026

📝 Walkthrough

Walkthrough

Replaced direct currentActivity references with reactApplicationContext.currentActivity across multiple code paths in the Google Auth Module. The change affects activity retrieval for sign-in flows, error dialogs, and validation checks while maintaining similar control flow.

Changes

Cohort / File(s) Summary
Activity Reference Source Update
android/src/main/java/com/googleauth/GoogleAuthModule.kt
Refactored to obtain activity references from reactApplicationContext.currentActivity instead of direct currentActivity property in silent/interactive sign-in flows, error dialog displays, and activity validation checks

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hopped through code so fine,
Changed where activities aligned,
From direct paths to context's way,
The sign-ins dance a cleaner play,
Four little lines, refactored bright! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.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 directly describes the main change—updating getCurrentActivity references to use reactApplicationContext.currentActivity in GoogleAuthModule.kt, which aligns with the core objective of modernizing activity access patterns.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

@channel101
Copy link
Copy Markdown
Contributor Author

@sbaiahmed1

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
android/src/main/java/com/googleauth/GoogleAuthModule.kt (2)

303-307: Consider using getValidActivity() for consistent activity validation.

The getTokens method checks only for null activity, while signIn (line 189) uses the getValidActivity() helper that also validates the activity isn't finishing or destroyed. Using getValidActivity() consistently would prevent potential issues if the activity state changes between the check and usage in performSilentSignIn.

♻️ 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 using getValidActivity() for consistent activity validation.

Similar to getTokens, this method only checks for null activity. Using the getValidActivity() 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

📥 Commits

Reviewing files that changed from the base of the PR and between 9a0fd29 and 9d3a337.

📒 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 let is 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. The getValidActivity() 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.

@channel101
Copy link
Copy Markdown
Contributor Author

@sbaiahmed1 Ready to go!

@sbaiahmed1
Copy link
Copy Markdown
Owner

Hello @channel101 , Thank you for your contribution :D
I really appretiate it
I will test this tonight and merge and release it

@channel101
Copy link
Copy Markdown
Contributor Author

Hello @channel101 , Thank you for your contribution :D
I really appretiate it
I will test this tonight and merge and release it

Thanks!

@channel101
Copy link
Copy Markdown
Contributor Author

@sbaiahmed1 ?

@sbaiahmed1 sbaiahmed1 merged commit f221db4 into sbaiahmed1:main Jan 12, 2026
6 checks passed
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.

2 participants