-
Notifications
You must be signed in to change notification settings - Fork 159
Fix buggy activity count logic #1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses issues with the activity count logic in the BranchActivityLifecycleObserver by adjusting log output and resetting the activity counter when necessary. Key changes include:
- Removing the activities-on-stack information from some log statements while adding separate logs for activity count and activities on stack.
- Resetting activityCnt_ to 0 when it is negative to prevent unintended session clearance.
- Adding additional debug logging in onActivityResumed, onActivityPaused, onActivityStopped, and onActivityDestroyed.
| public void onActivityResumed(@NonNull Activity activity) { | ||
| Branch branch = Branch.getInstance(); | ||
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch + " Activities on stack: " + activitiesOnStack_); | ||
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch); |
Copilot
AI
May 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider including the activitiesOnStack_ information in the onActivityResumed log for consistency with the logging in other lifecycle methods, as it can aid in debugging the activity stack state.
| In such cases, activityCnt_ could be set to -1, which could cause the above line to clear | ||
| session parameters. Just reset to 0 if we're here. | ||
| */ | ||
| activityCnt_ = 0; |
Copilot
AI
May 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While resetting activityCnt_ to 0 is a defensive measure for negative values, consider also logging a warning when such a reset occurs to help diagnose unexpected lifecycle inconsistencies.
| activityCnt_ = 0; | |
| activityCnt_ = 0; | |
| BranchLogger.w("Unexpected lifecycle inconsistency: activityCnt_ was negative and has been reset to 0."); |
ReferenceSDK-XXX -- Fix buggy activity count logic. Summary By MatterAI
🔄 What Changed
🔍 Impact of the ChangeThis change prevents the activity counter from going negative, which could cause session parameters to be incorrectly cleared. The fix ensures proper session management across different app integration patterns. 📁 Total Files Changed1 file modified: 🧪 Test AddedN/A - No explicit test additions in the PR. 🔒Security VulnerabilitiesN/A - No security vulnerabilities detected. Testing InstructionsRisk Assessment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR addresses an important bug in the activity count logic. The changes look good overall, with improved logging and a fix for the negative counter issue. I have a few suggestions to further improve the implementation.
| public void onActivityResumed(@NonNull Activity activity) { | ||
| Branch branch = Branch.getInstance(); | ||
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch + " Activities on stack: " + activitiesOnStack_); | ||
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: The logging statements are added individually for each lifecycle method, which could lead to inconsistent logging patterns.
Fix: Consider extracting the logging into a helper method to ensure consistency.
Impact: Improves code maintainability and ensures consistent logging patterns across lifecycle methods.
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch); | |
| BranchLogger.v("onActivityResumed, activity = " + activity + " branch: " + branch + ", activityCnt_: " + activityCnt_ + ", activitiesOnStack_: " + activitiesOnStack_); |
| if (activityCnt_ < 1) { | ||
| branch.setInstantDeepLinkPossible(false); | ||
| branch.closeSessionInternal(); | ||
|
|
||
| /* It is possible some integrations do not call Branch.getAutoInstance() before the first | ||
| activity's lifecycle methods execute. | ||
| In such cases, activityCnt_ could be set to -1, which could cause the above line to clear | ||
| session parameters. Just reset to 0 if we're here. | ||
| */ | ||
| activityCnt_ = 0; | ||
| BranchLogger.v("activityCnt_: reset to 0"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Bug Fix
Issue: The code resets activityCnt_ to 0 when it's less than 1, but doesn't handle the case where it might be exactly 0.
Fix: Add a more explicit condition to only reset when negative.
Impact: Prevents unnecessary resets and makes the intent of the code clearer.
| if (activityCnt_ < 1) { | |
| branch.setInstantDeepLinkPossible(false); | |
| branch.closeSessionInternal(); | |
| /* It is possible some integrations do not call Branch.getAutoInstance() before the first | |
| activity's lifecycle methods execute. | |
| In such cases, activityCnt_ could be set to -1, which could cause the above line to clear | |
| session parameters. Just reset to 0 if we're here. | |
| */ | |
| activityCnt_ = 0; | |
| BranchLogger.v("activityCnt_: reset to 0"); | |
| if (activityCnt_ < 1) { | |
| branch.setInstantDeepLinkPossible(false); | |
| branch.closeSessionInternal(); | |
| /* It is possible some integrations do not call Branch.getAutoInstance() before the first | |
| activity's lifecycle methods execute. | |
| In such cases, activityCnt_ could be set to -1, which could cause the above line to clear | |
| session parameters. Just reset to 0 if we're negative. | |
| */ | |
| if (activityCnt_ < 0) { | |
| activityCnt_ = 0; | |
| BranchLogger.v("activityCnt_: reset from negative to 0"); | |
| } | |
| } |
| BranchLogger.v("activityCnt_: reset to 0"); | ||
| } | ||
|
|
||
| BranchLogger.v("activitiesOnStack_: " + activitiesOnStack_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚡️ Performance Improvement
Issue: The logging of activitiesOnStack_ collection might be expensive if the collection is large.
Fix: Add a conditional check to only log in debug builds or when verbose logging is enabled.
Impact: Reduces unnecessary string concatenation and logging overhead in production builds.
| BranchLogger.v("activitiesOnStack_: " + activitiesOnStack_); | |
| if (BranchLogger.isDebugEnabled()) { | |
| BranchLogger.v("activitiesOnStack_: " + activitiesOnStack_); | |
| } |
| public void onActivityDestroyed(@NonNull Activity activity) { | ||
| Branch branch = Branch.getInstance(); | ||
| BranchLogger.v("onActivityDestroyed, activity = " + activity + " branch: " + branch + " Activities on stack: " + activitiesOnStack_); | ||
| BranchLogger.v("onActivityDestroyed, activity = " + activity + " branch: " + branch); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Optional Recommendation
Issue: The logging statements for activity lifecycle events are scattered throughout the code.
Fix: Consider adding a debug flag to control all lifecycle logging.
Impact: Makes it easier to enable/disable all lifecycle logging at once for debugging purposes.
| BranchLogger.v("onActivityDestroyed, activity = " + activity + " branch: " + branch); | |
| BranchLogger.v("onActivityDestroyed, activity = " + activity + " branch: " + branch); | |
| // TODO: Consider adding a debug flag to control all lifecycle logging |
Reference
SDK-XXX -- <TITLE>.
Description
Testing Instructions
Risk Assessment [
HIGH||MEDIUM||LOW]Reviewer Checklist (To be checked off by the reviewer only)
cc @BranchMetrics/saas-sdk-devs for visibility.