4

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
    }
}
spirytus
  • 10,726
  • 14
  • 61
  • 75

4 Answers4

3

Once your activity receives the onPause message it means that the user might be using or going to use some other application .. in this case if he tries to use camera through other application the camera must be in freed or released by your application.

bleater
  • 5,098
  • 50
  • 48
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • 1
    This is the real reason. The new activity will be started BEFORE your activity gets the onStop() message. So if you don't release the camera in onPause(), and the new activity wants to read the camera, it will get an error. – bleater Apr 03 '13 at 01:19
2

according to application lifecycle

Paused
    Another activity is in the foreground and has focus, but this one is 
            still visible. That is, another activity is visible on top of this 
            one and that activity is partially transparent or doesn't cover the 
            entire screen. (...)

I think the documentation follows the rule of the thumb "release resources as soon as possible": onPause is earlier than onStop.

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

k3b
  • 14,517
  • 7
  • 53
  • 85
  • right, I just checked my default camera app. on my Android phone (Samsung Infuse) and it seems when I start recording and I hold down "home" button to get to "recent apps. popup window" the recording doesn't stop. Am I right in thinking it releases camera in onStop rather then onPause? Also it does actually cause trouble as when from "recent apps. window" I navigate then to my app. which requests camera I get exception. – spirytus Aug 07 '12 at 04:38
0

Since onStop() is not guaranteed to be called, you can't always do in onStop() what is done in onPause().

For Detail Answer

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause(). onStop() may be called after onPause(), or it may not. Depends on the situation.

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
-1

onPause would mean your activity is no longer visible.

onStop would only be called if Android didn't think your process was needed anymore.

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • hmm.. as I understand in onPause my activity is still partially visible and only in onStop is fully covered. In onStop activity is still running as normal as far as I know. I also think that until android Honeycomb activity might be destroyed either in onPause or onStop if resources are needed. – spirytus Aug 07 '12 at 04:30