I seem to be missing something as I fail to understand why in Android documentation (Android Camera doc. link) it is recommended to release Camera object (as well as MediaRecorder) in onPause() Activity callback? Activity still might be visible by that time and Camera might be running preview so why the Camera object would be released in onPause() rather then onStop() when activity is already hidden? I understand that MediaRecorder object could be stopped in onPause() but Camera itself doesn’t make sense to me. What am I missing here? Piece of code from Android documentation is below (its under Releasing the Camera heading):
@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
according to application lifecycle
I think the documentation follows the rule of the thumb “release resources as soon as possible”:
onPauseis earlier thanonStop.camera in the background window needs energy while the user has to to pay attentions to the popup.
Camera in the backgroud is of course more comfortable but for a mobile battery life time is more important.
The popup that intercepted you activity might need the camera and/or might need a lot of memory.
In your scenario when the camera should continue recording in the background the camera-s lifecycle and recording should be controlled by a service