Android: Activity getting Destroyed after calling Camera Intent
Asked Answered
F

6

49

I have two Activities, A1 and A2. A1 calls A2 and from A2, I am calling the camera intent as below:

launchIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoPath);   
startActivityForResult(launchIntent,CAMERA_REQUEST);

It opens the camera, and I can take the picture. The problem arises once I click the save button (tick button in s3). My onActivityResult function is not called; instead, A2's onDestroy method is called. I have few steps to be done in the onActivityResult function.

I have my manifest like this for my second Activity (A2):

android:configChanges="keyboardHidden|orientation|locale"
android:screenOrientation="portrait

In HTC One X, my onActivityResult function is getting called, but in my S3 second Activity(A2) is getting destroyed.

How can I fix this?

Fencible answered 15/4, 2013 at 12:16 Comment(4)
is that screenOrientation needed?Matos
Yes i need my app to be in potrait :(Fencible
@Surdarshan hey! do you find any working solution?Shellfish
you might want to check regarding noHistory=true or ` Intent.FLAG_ACTIVITY_NO_HISTORY. See here for details: https://mcmap.net/q/356075/-onactivityresult-not-called-after-taking-a-photo-in-androidElena
C
84

I have found a solution in this SO post. The issue is that when you click on the "save" button of the camera, the activity call changes the orientation method and it will be destroyed and recreated. Try to set:

android:configChanges="orientation|screenSize"

in the android manifest (not only android:configChanges="orientation" as suggested in this other SO post; it not work for API level 13 or higher).

It prevented the destroy activity for me.

Concealment answered 14/5, 2013 at 14:21 Comment(3)
android:configChanges="orientation|keyboardHidden|screenSize" #16154779Unwrap
Thanks man, you saved me android:configChanges="orientation|keyboardHidden|screenSize" working perfect.Noami
I was facing same issue and found that in developer option "Do not keep activities" check were marked. comment for help of other developers.Berkman
E
45

Launching camera requires a lot of memory. So on devices with low memory android system closes the Activities running in background and hence onCreate() is called. Due to this photopath you have given becomes null and you wont be able to get the saved image.

Solution is to save the photopath while system is destroying your activity and then restore it again.

@Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub

            outState.putString("photopath", photopath);


        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("photopath")) {
                photopath = savedInstanceState.getString("photopath");
            }
        }

        super.onRestoreInstanceState(savedInstanceState);
    }

and in case you are doing this on Fragment.

@Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub

            outState.putString("photopath", photopath));


        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("photopath")) {
                photopath = savedInstanceState.getString("photopath");
            }
        }

        super.onViewStateRestored(savedInstanceState);
    }
Epistemic answered 3/11, 2014 at 5:24 Comment(5)
This is the correct answer and this solve the problem. Thanks to give the solution for activity and fragment both. Really nice answer.Exasperate
this approach is much betterQuadrivial
I am facing the same problem, but my fragment's onSaveInstanceState never gets called. Only my activity's onDestory method gets called. Any solutions?Bash
Can you validate your statement, that low memory devices closes activities in backround, by posting some google developer document links.Zoologist
Some users are facing this problem but I can't reproduce it. Is it possible to force or emulate this condition ?Smilacaceous
R
28

Be sure you don't have the "Don't keep activities" Developer setting on, as it will destroy the activity you are leaving.

Rosannrosanna answered 17/11, 2013 at 5:50 Comment(4)
I just wasted A DAY of work trying to figure out what is wrong with my app. Thanks a bunch!Birmingham
But your App should still work and not crash with "Don't keep activities" on, shouldn't it?Conduplicate
Yes, but that is not normal behavior. If you launched an activityForResult and lose Activity A, result of Camera intent won't be able to rebuild activity A anyway.Rosannrosanna
Android should be able to re-create it, that's what onCreate() and onSavedInstanceState() are forBindery
U
13

The camera app requires a lot of memory and to free up memory, the operating system has to kill background apps, including yours. This is normal for all Android apps. Your activity will be recreated when the camera app returns. To retain activity state information, override onSaveInstanceState() to store your data and read them back in onCreate().

Unbated answered 15/4, 2013 at 12:27 Comment(2)
ya i too expected the same behaviour as it ll call the onCreate method again but it didnt call the on create of A2Fencible
Can you post the google developer document on your statement.for reference please.Zoologist
H
2

My Activity has many tasks running when I am calling Camera Intent.. I have GPS location listeners, and many more tasks.

When it returns from the Camera Intent I need to add marker on a map in the location that the image was taken.. So if the user took more than one picture, it should show markers for each picture..

If it will destroy my Activity each time I call Camera Intent all the information that I had before about markers and their location will be lost.. And also, I will have to initialize the map again. That's a big problem for my app.

And yes, it only happens when I take a picture in Landscape mode. If I take the picture in portrait everything is fine so far..

I will try to use android:configChanges="orientation|screenSize" As suggested here.. Hope it will help!

Hedva answered 26/2, 2016 at 9:20 Comment(1)
Well, after add this the manifest, I can say that the problem was gone!! Cheers!. In addition, if this was related to memory, so it should happen also when I am taking portrait images and as I said, it only happens on landscape so it highly make sense that it's because of the orientation and not memory.Hedva
D
2

For me, it happens when i enable an option in Developer Mode called "Don't keep Activities". After i unchecked it, activity won't recreate while backing from choosing a picture from gallery or from camera app.

Didache answered 17/7, 2018 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.