Fix: Prevent NPE when toBack=true and user taps before preview is ready (Android)
#414
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.
Problem
When
toBack=true, the plugin forwards WebView touch events to the native camera container to support pinch-to-zoom.Previously, the WebView
OnTouchListenerwas installed duringstartCamera(), before theCameraActivityfragment view was created.Because
FragmentTransaction.commit()is asynchronous, there is a short window where:fragment != null(fragment instance created)fragment.toBack == true(option applied)fragment.frameContainerLayout == null(initialized later inCameraActivity.onCreateView())If the user tapped during that window, the touch-forwarding path called:
…and crashed with:
Changes
This PR eliminates the race and adds defense-in-depth:
1) Install touch-forwarding only after the camera/fragment is ready
setupBroadcast()call fromstartCamera().setupBroadcast()inonCameraStarted()(before resolving the saved start call), ensuring the fragment view hierarchy exists andframeContainerLayoutis initialized.2) Add a null guard in the touch-forwarding path
Touch forwarding now checks:
fragment != nullfragment.toBack == truefragment.frameContainerLayout != nullIf not ready, touch events are simply not forwarded (no crash).
3) Remove the WebView touch listener on stop
stop()now clears the WebView touch listener via:getBridge().getWebView().setOnTouchListener(null);This prevents stale listeners from persisting across stop/start cycles or teardown.
Why this works
The listener is now installed only after
CameraActivityhas reached a state where its view is created and the camera preview is started, removing the timing window that previously allowedframeContainerLayoutto be null.Manual test checklist
toBack=true: callstartCamera()and tap immediately/repeatedly while the preview is launching → no crashtoBack=true: once preview is visible, pinch-to-zoom still worksstopCamera()and tap afterward → no crash, listener removedstart → stop → startmultiple times → stable, no listener leaks/crashes