diff --git a/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java b/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java index 2ebd6476..83ef9ad7 100644 --- a/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +++ b/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java @@ -370,27 +370,18 @@ public void run() { fragmentTransaction.add(containerView.getId(), fragment); fragmentTransaction.commit(); - call.resolve(); + // NOTE: we don't return invoke call.resolve here because it must be invoked in onCameraStarted + // otherwise the plugin start method might resolve/return before the camera is actually set in CameraActivity + // onResume method (see this line mCamera = Camera.open(defaultCameraId);) and the next subsequent plugin + // method invocations (for example, getSupportedFlashModes) might fails with "Camera is not running" error + // because camera is not available yet and hasCamera method will return false + // Please also see https://developer.android.com/reference/android/hardware/Camera.html#open%28int%29 + bridge.saveCall(call); + cameraStartCallbackId = call.getCallbackId(); } else { call.reject("camera already started"); } } - - FragmentManager fragmentManager = getBridge().getActivity().getFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - fragmentTransaction.add(containerView.getId(), fragment); - fragmentTransaction.commit(); - - // NOTE: we don't return invoke call.resolve here because it must be invoked in onCameraStarted - // otherwise the plugin start method might resolve/return before the camera is actually set in CameraActivity - // onResume method (see this line mCamera = Camera.open(defaultCameraId);) and the next subsequent plugin - // method invocations (for example, getSupportedFlashModes) might fails with "Camera is not running" error - // because camera is not available yet and hasCamera method will return false - // Please also see https://developer.android.com/reference/android/hardware/Camera.html#open%28int%29 - bridge.saveCall(call); - cameraStartCallbackId = call.getCallbackId(); - } else { - call.reject("camera already started"); } ); }