Skip to content

Conversation

@nan-li
Copy link
Contributor

@nan-li nan-li commented Dec 5, 2025

Description

One Line Summary

Fix reported crashes when handlers / observers are removed on destroy and the SDK is uninitialized or cleaned up already.

Details

To address #1554

  • When the activity is destroyed, the code tries to remove handlers by calling OneSignal.getInAppMessages(), but the native OneSignal SDK may not be fully initialized yet (or may have been cleaned up), causing it to throw "Must call 'initWithContext' before use".
  • Wrap removeHandlers() and removeObservers() calls in try-catch blocks within onHostDestroy() and onCatalystInstanceDestroy() to handle cases where the native OneSignal SDK throws "Must call 'initWithContext' before use" during activity destruction.
  • Also move the oneSignalInitDone flag further down past the native OneSignal.initWithContext(context, appId) call. The native SDK already handles if initWithContext is called while it is still initializing.

Motivation

Bug fix, fix crash

Scope

When activity being destroyed

Testing

Manual testing

Manually ran app in Android 35 emulator, app initializes, gets IAMs, get notifications.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

* Wrap removeHandlers() and removeObservers() calls in try-catch blocks within onHostDestroy() and onCatalystInstanceDestroy() to handle cases where the native OneSignal SDK throws "Must call 'initWithContext' before use" during activity destruction.
* Also move the oneSignalInitDone flag further down past the native `OneSignal.initWithContext(context, appId)` call.
@nan-li nan-li requested a review from a team as a code owner December 5, 2025 02:33
@nan-li nan-li requested review from a team and removed request for a team December 5, 2025 02:36
@fadi-george
Copy link
Collaborator

Change seem reasonable. I would consider updating/adding test for these.

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.

3 participants