Cancel camera after selecting intent canceled

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.

+4
source share
1 answer

That's right, you opened your camera in the onStart method and release it in the onStop method of your activity. The onResume and onPause methods are part of the visible life cycle of android activity. OnStop is called when another activity takes up all of the visible space. OnPause is even called when another action comes to the fore, even if it does not occupy all of the visible space, such as the intent dialog box when it appears. Therefore, I suggest that moving your camera and releasing it into the correct life methods should do the trick. Here you can find additional information about the life cycle of an activity, but I'm sure you are familiar with this:

http://developer.android.com/reference/android/app/Activity.html

0
source

Source: https://habr.com/ru/post/1379820/


All Articles