I created a custom CameraView camera CameraView , which extends SurfaceView and also implements the SurfaceHolder.Callback interface. View works with the camera. When you open the view, it shows a preview of the camera. On the same screen there is an overlay with two buttons - "Shooting", "Select from gallery". The activity that contains the CameraView releases and reopens the camera in the onPause() and onResume() methods.
If I click the "Select from gallery" button, the following intention will be 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 intention, then it will be good. The operation automatically opens, the camera is released. I can also hit the gallery and I will return to CameraView activity and the camera preview will restore.
The interesting part begins if there are several actions that can handle this intention, and a dialog box for choosing the intent opens. When the intent dialog appears, onPause() is called in the parent activity, and the camera is released, the screen turns black. If I don’t choose an intention from the dialogue, but instead click the "Back" button on the phone onResume() , but viewing the camera never returns. To view the camera preview again, I need to go back to the previous step and return to the preview action.
The next problem arises because only onPause() is called when the dialog onPause() , but if I really switch to another activity surfaceDestroyed() , it is also called. The same is true for onResume() , when the dialog is canceled using the back button, surfaceChanged() and surfaceCreated() never called.
My question is how to get a camera preview if the intent dialog is canceled. Is there a way how to call SurfaceHolder.Callback methods explicitly? I know that SurfaceView has hidden hideSurface() and showSurface() , but I don’t want to go this route.