I have created a custom camera preview view CameraView
which extends SurfaceView
, and it also implements SurfaceHolder.Callback
interface. The view operates with the camera. When you open the view it shows a camera preview. On the same screen there is also overlay with two buttons - 'Take picture', 'Choose from gallery'. The activity that holds the CameraView
releases and reopens the camera in onPause()
and onResume()
methods.
If I click the 'Choose from gallery' button, the following intent is created:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, LOAD_PICTURE);
If there is only one activity that can respond to this intent then it's fine. The activity gets automatically opened, the camera is released. I can also hit back when on the gallery and I will get back into the CameraView
activity, and camera preview is restored.
The interesting part starts if there are multiple activities that can handle this intent, and the intent chooser dialog pops up. When intent chooser dialog spawns, onPause()
gets called in parent activity and camera gets released, the screen becomes black. If I dont choose intent from the dialog, but instead click back button on the phone onResume()
is called, but camera preview is never coming back. To get the camera preview to show again, I need to go back to previous activity and go back inside the preview activity.
The following problem happens because, when the dialog is raised only onPause()
gets called, but if I actually switch to different activity surfaceDestroyed()
get called also. The same is true for onResume()
when the dialog is canceled with back button, surfaceChanged()
and surfaceCreated()
never gets called.
My question is how to get camera preview to reappear if the intent chooser dialog is canceled. Is there a way how to trigger SurfaceHolder.Callback
methods explicitly?
I know that there are hidden hideSurface()
and showSurface()
in SurfaceView
, but I don't want to go this route.