2

I've run into a weird occurrence with regards to my child activity's life-cycle methods. If I launch and close the activity enough times, there eventually comes a point where for some reason, upon launching, onPause gets called immediately after onCreate (skipping onResume), which is then followed by onResume being called twice in a row. This ends up causing a RuntimeException in my onResume.

Here's the code that starts my activity:

Intent intent = new Intent(MainActivity.this, RecorderActivity.class);
startActivityForResult(intent, AV_CAPTURE);

My onPause:

protected void onPause() {
    camera.stopPreview();
    camera.setPreviewCallback(null);
    camera.release();
    camera = null;
    super.onPause();
};

I'm not really sure what else I can provide here, it's all pretty straightforward. finish() is called to close the activity, etc.

I should also probably mention that the activity is being triggered from a Javascript Interface attached to a WebView. Another interesting thing to mention is that setting a breakpoint anywhere in the first code block above seems to prevent the issue from happening.

Drazen Bjelovuk
  • 5,201
  • 5
  • 37
  • 64
  • But is it a problem, actually? – Shlublu May 28 '14 at 16:02
  • I mean, I could probably work around it by wrapping `Camera.open()` in a try catch, but I'd really like to know what's going on. – Drazen Bjelovuk May 28 '14 at 16:12
  • I see. @Uxonith post makes complete sense as anyway exceptions should be catched. I think, without being sure, that the lauching/closing of activities is too fast so the device is overwhelmed. – Shlublu May 28 '14 at 16:17
  • Doesn't seem to matter how fast I do it though. – Drazen Bjelovuk May 28 '14 at 16:27
  • 2
    Strange. I have re-read this and it seems that there is no guarantee that onPause() / onResume() are called just once. Just a guarantee they are called, and just an expectation they are once. http://developer.android.com/training/basics/activity-lifecycle/index.html – Shlublu May 29 '14 at 09:08

1 Answers1

3

While I do not have an answer for the resume being called twice, I do have a link to the Android docs stating that you should always check for exceptions while attempting to open the camera. See here:

http://developer.android.com/guide/topics/media/camera.html#access-camera

In your onPause, make sure that you set your Camera object to null. Then in onResume, only attempt to call getCameraInstance() if your camera is not null.

Uxonith
  • 1,602
  • 1
  • 13
  • 16