fix: add FragmentActivity check in RNPaywallsModule (fixes #1127) #1186
+11
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What and Why
This PR addresses an issue where the paywall presentation on Android can lead to crashes when the current activity is not a FragmentActivity or when the PaywallView is not fully initialized. The changes introduced aim to:
Delayed Initialization:
By ensuring that the view is fully initialized (using techniques like posting to the view), we can attach listeners only after the PaywallView is attached to the window. This minimizes the risk of lifecycle-related crashes during asynchronous operations or UI updates.
FragmentActivity Check:
The updated code now verifies whether the current activity is an instance of FragmentActivity. If it isn’t, an error is logged and the promise is rejected with the code E_ACTIVITY_NOT_FRAGMENT. This prevents the paywall from being presented in an unsupported activity type, which could otherwise lead to runtime crashes.
Additional Considerations
Testing:
This change has not been fully tested yet. Once integrated, please ensure thorough testing across various Android devices and configurations to confirm that the paywall presentation behaves correctly without crashes.
Logging:
Enhanced logging is in place to help diagnose if an unsupported activity type is encountered, which should aid in future debugging efforts.
Thank you for contributing to react-native-purchases. Before pressing the "Create Pull Request" button, please provide the following:
A description about what and why you are contributing, even if it's trivial.
The issue number(s) or PR number(s) in the description if you are contributing in response to those.
If applicable, unit tests.