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?

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
Sudarshan
  • 1,291
  • 3
  • 26
  • 35

6 Answers6

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.

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
giacomo87
  • 841
  • 5
  • 3
  • 5
    android:configChanges="orientation|keyboardHidden|screenSize" http://stackoverflow.com/questions/16154279/galaxy-s3-taking-picture-in-portrait-mode-destroys-activity – NBApps Nov 13 '14 at 15:13
  • Thanks man, you saved me android:configChanges="orientation|keyboardHidden|screenSize" working perfect. – sandeepmaaram Aug 20 '15 at 09:55
  • 1
    I was facing same issue and found that in developer option "Do not keep activities" check were marked. comment for help of other developers. – Khizar Hayat May 05 '17 at 11:05
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);
    }
Rajesh Narwal
  • 898
  • 8
  • 8
  • 3
    This is the correct answer and this solve the problem. Thanks to give the solution for activity and fragment both. Really nice answer. – Smeet Nov 13 '14 at 17:49
  • Really really helpful! Thank you! – Lukas Dec 04 '15 at 09:34
  • this approach is much better – Jemshit Dec 07 '15 at 10:36
  • 2
    I am facing the same problem, but my fragment's onSaveInstanceState never gets called. Only my activity's onDestory method gets called. Any solutions? – Sandra Jan 25 '16 at 09:50
  • 1
    Can you validate your statement, that low memory devices closes activities in backround, by posting some google developer document links. – Sandeep R Mar 15 '16 at 06:38
  • Some users are facing this problem but I can't reproduce it. Is it possible to force or emulate this condition ? – fegoulart Aug 27 '20 at 15:05
27

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

Omaraf
  • 833
  • 8
  • 15
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().

laalto
  • 150,114
  • 66
  • 286
  • 303
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!

Moti Bartov
  • 3,454
  • 33
  • 42
  • 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. – Moti Bartov Feb 26 '16 at 13:53
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.

Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74