androidandroid-cameraandroid-camera2

Camera2 Lifecycle - close device on minimize?


What is the recommended way of dealing with Camera2's lifecycle?

When I open a Camera session, I see three steps:

  1. Open CameraDevice
  2. Open CameraCaptureSession
  3. Start a repeating CaptureRequest

Now when the user minimizes the app, or when I want to shortly pause the Camera, what do I do? Do I just stop the repeating CaptureRequest? Do I close the CameraCaptureSession? Or do I also fully close the CameraDevice?

On iOS, I can simply "deactivate" the Camera session to keep everything warm, but I'm not sure if that is the equivalent of just stopping the repeating request on Android. Opening a CameraCaptureSession is also quite fast.


Solution

  • If you don't have a foreground service, and your app goes into the background, you'll eventually have your camera device get disconnected by the OS, as a privacy measure. But that takes several seconds.

    If you want the camera privacy indicator to go away, then you need to at least stop the repeating capture request. Then you can wait a second or two to close the camera device, if you want to try to enable fast return to camera operation if the user comes right back to your app.

    Closing the session doesn't really have any impact on the privacy indicator or power, so I don't see a lot of value on doing that, except that if you have to create a new session anyway when the user returns to the app (a new preview Surface for example), then you might as well close the session to save a little bit of time on the restart (though it's not much - replacing a session is nearly as fast as creating a new one).