Skip to content

Commit a7e7061

Browse files
authored
fix: Close CameraSession if the View is removed (#2174)
* fix: Close `CameraSession` if the View is removed * fix: Use ViewManager's `onDropViewInstance` instead * fix: Only stop repeating if we have a session * fix: Reset `configuration` on `close()`
1 parent 98a6417 commit a7e7061

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

package/android/src/main/java/com/mrousavy/camera/CameraView.kt

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class CameraView(context: Context) :
131131
super.onDetachedFromWindow()
132132
}
133133

134+
fun destroy() {
135+
cameraSession.close()
136+
}
137+
134138
fun update() {
135139
Log.i(TAG, "Updating CameraSession...")
136140

package/android/src/main/java/com/mrousavy/camera/CameraViewManager.kt

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class CameraViewManager : ViewGroupManager<CameraView>() {
3131

3232
override fun getName(): String = TAG
3333

34+
override fun onDropViewInstance(view: CameraView) {
35+
view.destroy()
36+
super.onDropViewInstance(view)
37+
}
38+
3439
@ReactProp(name = "cameraId")
3540
fun setCameraId(view: CameraView, cameraId: String) {
3641
view.cameraId = cameraId

package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
172172
codeScannerOutput?.close()
173173
codeScannerOutput = null
174174

175+
configuration = null
175176
isRunning = false
176177
}
177178

@@ -363,16 +364,16 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
363364
}
364365

365366
private fun configureCaptureRequest(config: CameraConfiguration) {
366-
val device = cameraDevice ?: throw NoCameraDeviceError()
367-
val captureSession = captureSession ?: throw CameraNotReadyError()
368-
369367
if (!config.isActive) {
370368
// TODO: Do we want to do stopRepeating() or entirely destroy the session?
371369
// If the Camera is not active, we don't do anything.
372-
captureSession.stopRepeating()
370+
captureSession?.stopRepeating()
373371
return
374372
}
375373

374+
val device = cameraDevice ?: throw NoCameraDeviceError()
375+
val captureSession = captureSession ?: throw CameraNotReadyError()
376+
376377
val previewOutput = previewOutput
377378
if (previewOutput == null) {
378379
Log.w(TAG, "Preview Output is null, aborting...")

0 commit comments

Comments
 (0)