Navaron Bracke

fix bug with unbinding camera observers; release scanner on stop

@@ -422,14 +422,27 @@ class MobileScanner( @@ -422,14 +422,27 @@ class MobileScanner(
422 } 422 }
423 423
424 val owner = activity as LifecycleOwner 424 val owner = activity as LifecycleOwner
425 - camera?.cameraInfo?.torchState?.removeObservers(owner) 425 + // Release the camera observers first.
  426 + camera?.cameraInfo?.let {
  427 + it.torchState.removeObservers(owner)
  428 + it.zoomState.removeObservers(owner)
  429 + it.cameraState.removeObservers(owner)
  430 + }
  431 + // Unbind the camera use cases, the preview is a use case.
  432 + // The camera will be closed when the last use case is unbound.
426 cameraProvider?.unbindAll() 433 cameraProvider?.unbindAll()
427 - textureEntry?.release()  
428 - 434 + cameraProvider = null
429 camera = null 435 camera = null
430 preview = null 436 preview = null
  437 +
  438 + // Release the texture for the preview.
  439 + textureEntry?.release()
431 textureEntry = null 440 textureEntry = null
432 - cameraProvider = null 441 +
  442 + // Release the scanner.
  443 + scanner?.close()
  444 + scanner = null
  445 + lastScanned = null
433 } 446 }
434 447
435 private fun isStopped() = camera == null && preview == null 448 private fun isStopped() = camera == null && preview == null