4

I try open camera following way:

...
    private void runCamera() {
        String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imageFile = new File(Singleton.instanse.mPushFilePath);
            mImageFileUri = Uri.fromFile(imageFile);
            intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mImageFileUri);
            startActivityForResult(intent, CAMERA_RESULT);
    }
...

if I run this method run next methods:

07-16 19:46:22.264: I/System.out(6875): -onPause
07-16 19:46:26.104: I/System.out(6875): -onStop

I make photo, end run next methods:

07-16 19:46:41.217: I/System.out(6875): -onDestroy
07-16 19:46:41.284: I/System.out(6875): -onCreate
07-16 19:46:41.291: I/System.out(6875): -onStart
07-16 19:46:41.295: I/System.out(6875): -onActivityResult
07-16 19:46:41.295: I/System.out(6875): -onResume
07-16 19:46:41.295: I/System.out(6875): -onPostResume
07-16 19:46:41.522: I/System.out(6875): -onPause
07-16 19:46:41.522: I/System.out(6875): -onStop
07-16 19:46:41.522: I/System.out(6875): -onDestroy
07-16 19:46:41.604: I/System.out(6875): -onCreate
07-16 19:46:41.612: I/System.out(6875): -onStart
07-16 19:46:41.616: I/System.out(6875): -onResume
07-16 19:46:41.616: I/System.out(6875): -onPostResume

Why onDestroy run twiсe? How to fix it? This problem found in Android 2.2. In Android 2.3.3 onDestroy never called!

sschuberth
  • 28,386
  • 6
  • 101
  • 146
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
  • Please use `android-activity` tag instead of `activity` tag :) – Alex Lockwood Jul 16 '12 at 15:59
  • 2
    this might be useful http://stackoverflow.com/questions/10411009/activity-killed-oncreate-called-after-taking-picture-via-intent/10411504#10411504 – Akram Jul 16 '12 at 16:13

3 Answers3

16

Actually the camera causes the orientation change in your activity that is why your activity is being destroyed and recreated.

Add this in your manifest file it will prevent the orientation change and your activity will not get destroyed and recreated.

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait" >
</activity>

Activity killed / onCreate called after taking picture via intent

Community
  • 1
  • 1
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
  • What do you do if you can't force the activity to be in a specific orientation? I'm running into a similar problem and can't force an orientation... – Justin Feb 26 '13 at 16:08
  • 1
    I'm running into a similar problem. I have this code in my activity. Launch my camera, but because my activity calls OnCreate before and after the OnActivityResult callback, the image is never seen in the view I put it unless the device is held in Landscape mode before the activity begins. – Andrew S May 27 '13 at 19:45
  • 3
    This is *not* a solution. If your device has not enough free RAM to keep your activity in memory when starting the camera (it happens…), the issue remains. – Marc Plano-Lesay Jan 27 '16 at 15:53
  • @MarcPlano-Lesay do you have any idea how can that be handled, I wanna keep my activity and call camera intent – Parag Pawar Dec 06 '19 at 06:49
  • @ParagPawar you have no way to enforce that. Android _will_ get rid of it if the memory is needed. Save your state in `onPause` and restore it in `onResume`. – Marc Plano-Lesay Dec 07 '19 at 06:12
4

onDestroy is not guaranteed to be called. Make sure you save persistent state in onPause rather than onStop and onDestroy. You should never rely on either onStop or onDestroy to be called.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
1

onDestroy is not guaranteed to be executed on any android version, if it does you should do something quick and return

also see this Activity OnDestroy never called?

Community
  • 1
  • 1
max4ever
  • 11,909
  • 13
  • 77
  • 115