简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM