Home » Android » android – Activity killed / onCreate called after taking picture via intent

android – Activity killed / onCreate called after taking picture via intent

Posted by: admin April 23, 2020 Leave a comment


I am trying to take a picture using an intent. My problem is that sometimes after taking a picture my activity, which calls startActivityForResult, seems to be destroyed so that onCreate is called again.

Here is my code for taking pictures after clicking an imageview, which image should be replaced:

if (!getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
        } else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_ITEM_PHOTO);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v(TAG, "onactivityresult called");
    if (requestCode == TAKE_ITEM_PHOTO) {
        if (data != null) {

            imageUri = data.getData();

                try {
                            getContentResolver(), imageUri));
            } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block

        } else
            Log.w(TAG, "data is null");

So all i try is to take a picture and replace the image of an imageview with it. But in some cases onCreate is called after onActivityResult was called and the new image is lost.

Help is greatly appreciated.

How to&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.

    android:screenOrientation="portrait" >


Fix the Orientation of your Activity/Application because when you back/finish() activity and at the same time you change orientation then activity refresh and restart again automatically.


It seems that there are phones that destroys the activity when importing an image, such as Galaxy S3. It is mentioned that if my app is in portrait mode it will happen because the images are in landscape mode. Thus, all the suggestions related to configChanges in the manifest file are not applicable to this situation.

What I ended up doing is instead of fighting the onDestroy of the activity (that caused the onActivityResult() of the fragment not to be called after onCreate()) was to implement onActivityResult() also in the activity itself and there I could get the image path. Then I passed that path to the fragment, once it is being created, for further processing. Of course I had to tell my app, once it is recreated, that it needs to go back to the calling fragmnet to process that image


Disable “Don’t keep activities” Developer setting. Otherwise it will destroy the activity you are leaving.