Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreate called before and after onActivityResult

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!

like image 643
ArtKorchagin Avatar asked Dec 01 '22 05:12

ArtKorchagin


2 Answers

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

like image 124
ArtKorchagin Avatar answered Dec 05 '22 01:12

ArtKorchagin


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.

like image 26
Alex Lockwood Avatar answered Dec 05 '22 02:12

Alex Lockwood