15

So, this question has been asked in many forms on this forum but none of the answers are helping me. I have a bug which I've only been able to reproduce on the Samsung Galaxy S3.

I want to simply take and store a photo using the phone's camera, but my Activity is being destroyed before I ever hear back from onActivityResult. This ONLY happens when I use the camera in portrait mode, in landscape mode it's fine.

I'm using this code to launch the camera:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, Config.ACTIVITY_TAKE_PHOTO);

In portrait mode, I never see a call to onActivityResult (from the Camera activity), I just see an onDestroy() called in my launching activity. There are no exceptions or errors in the log, and nothing obviously wrong to make it crash.

I've seen posts that recommend adding "android:configChanges="orientation|keyboardHidden"" to the Manifest, and doing lots of other things to manage the orientation change that occurs when using the camera, but nothing has helped so far.

Has anyone else solved this problem?

TomBomb
  • 3,236
  • 5
  • 31
  • 40
  • http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int). You can set the display orientation. I have a galaxy s3 i used the code on the developer site. I cannot replicate the problem you are facing. However i faced a different problem, the dispaly orientation was portrait, image was captured but the saved image was rotated. – Raghunandan Apr 22 '13 at 18:43
  • You should read this discussion and also realise that if you see onDestroy(), Android did not kill your process. Something else did. Please do read, and absorb, the Activity life cycle documentation and also realise that `android:configChanges="orientation|keyboardHidden` is a hack that hides many sins (bugs) unless you truly understand it. https://groups.google.com/forum/?fromgroups=#!topic/android-platform/x72BabVtWcQ – Simon Apr 22 '13 at 18:44
  • Simon, do you have any advice for how to get more info surrounding the onDestroy() of my activity? Is there anything I can do to print out a more detailed stack trace leading up to it? – TomBomb Apr 22 '13 at 18:48
  • `Thread.currentThread().getStackTrace()` – Simon Apr 22 '13 at 19:55
  • So it sounds like I should stop worrying about preventing the screen orientation change and resulting activity destruction, and focus more on recovering gracefully from the onDestroy after every image capture? – TomBomb Apr 23 '13 at 00:46
  • I see the same problem on a few models of S3 though. One works fine, while the others, onActivityResult is never called. Damn!!! – Kumar Bibek May 02 '13 at 07:19
  • @TomBomb you should paste logcat error massage – dharmendra Oct 14 '13 at 05:27
  • you might want to check regarding `noHistory=true` or ` `Intent.FLAG_ACTIVITY_NO_HISTORY`. See here for details: https://stackoverflow.com/a/62107555/3763032 – mochadwi May 30 '20 at 19:18
  • I would recommend to use open camera not the default camera app – Renz Carlo Nov 25 '22 at 16:18

2 Answers2

21

If you are targeting beyond API level 13, Adding

android:configChanges="orientation|keyboardHidden"

to the Manifest will not be enough.

Check this extract from Android documentation

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

Hence try adding

android:configChanges="orientation|keyboardHidden|screenSize"

to your Manifest file. This should solve your problem.

dishan
  • 1,346
  • 12
  • 21
  • @dishan I understand the orientation and screenSize flag here, but why keyboardHidden? – Tash Pemhiwa Apr 20 '14 at 07:51
  • @TashPemhiwa Yes for this particular question `keyboardHidden` may have no effect. Thus `android:configChanges="orientation|screenSize"` should also work. But again inclusion of `keyboardHidden` will provide you extra protection because it prevents your activity restart on a keyboard availability change event as well. `orientation` and `keyboardHidden` are the most commonly used configuration changes in Android that people want to handle themselves without restarting the activity. So having `keyboardHidden` there will be a wise thing to do. – dishan May 07 '14 at 09:45
1

Android can call the destroy() method in certain scenarios. One is when we use the method finish(). An alternative is to use the method isFinishing () from Activity class. If it really is being finalized could finalize their resources, otherwise, you can keep the values ​​allocated a class that inherits Application

@Override
protected void onDestroy() {

    final YourApp app = (YourApp) getApplicationContext();
    if (app != null && isFinishing()) {
        app.finalizeAppResources();
    }

    // ...

    super.onDestroy();
}
Douglas Frari
  • 4,167
  • 2
  • 22
  • 23