Lücks
Lücks

Reputation: 3984

Android Camera Intent calling onCreate again

I'm developing an application that calls the Camera, takes picture and create a thumbnail. It was working perfectly until now...I don't know for what reason, but now when I takes the picture and click on "Save" (android's default) the onCreate method is called.

So, the load of my thumbnail is not working anymore

I already tried to modify my Android manifest:

<uses-feature android:name="android.hardware.screen.portrait" />
<activity
        android:name=".MyActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait">
    </activity>

onCreate is still called.

Method to take picture:

public void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    FileHelper fileHelper = new FileHelper(this);
    fileUri = fileHelper.saveFile();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    // start the image capture Intent
    startActivityForResult(intent, ConstantsHelper.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

Is there any way to prevent that the "onCreate" is called after taking a picture?

Thanks!

Upvotes: 1

Views: 850

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134664

No. You just need to fix your code such that it works across the natural Activity lifecycle. Whether or not your previous Activity instance will stay alive when another comes to the foreground just depends on the device (how much memory is available to your app, whether "Don't Keep Activities" is checked under Developer Options, etc.)

It's a case that is perfectly valid, and expected to happen on the platform, and your Activity should be able to handle saving and restoring its state properly if it's killed off.

Whatever state you're initializing in onCreate(), just make sure you retain it in onSaveInstanceState(), and when onCreate() happens again, check if savedInstanceState is non-null. If it's not, restore whatever state you saved and continue on.

Upvotes: 2

Related Questions