35

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)) {
            Util.makeLongToast(R.string.lang_no_camera);
        } else {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_ITEM_PHOTO);
        }

...

@Override
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 {
                    img_photo.setImageBitmap(Media.getBitmap(
                            getContentResolver(), imageUri));
            } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        } 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.

Cattivo
  • 742
  • 2
  • 7
  • 15
  • 1
    possible duplicate of [Android: Activity getting Destroyed after calling Camera Intent](http://stackoverflow.com/questions/16014930/android-activity-getting-destroyed-after-calling-camera-intent) – Alex Cohn Sep 29 '15 at 20:24

4 Answers4

113

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.

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait" >
</activity>
Akram
  • 7,548
  • 8
  • 45
  • 72
  • 2
    Thank you! android:configChanges="orientation|keyboardHidden" was the missing line! ;) – Cattivo May 02 '12 at 18:16
  • 16
    THANK YOU for this answer! I'm hugging my monitor – Kenny C Nov 13 '12 at 21:02
  • 16
    Please also notice that in API upper than level 13, you should also add "screenSize" to configChanges. http://developer.android.com/guide/topics/manifest/activity-element.html – Wei Jiang Jun 18 '13 at 02:39
  • 4
    i m still having the same issue – Kalpesh Lakhani Apr 29 '14 at 09:17
  • This did solved the issue but its strange because my hosting activity already have ` android:screenOrientation="portrait"`. Also i notice the problem arise with camera apps that have fixed orientation of `landscape`! like samsung galaxy s3 have. – Muhammad Babar Dec 01 '14 at 09:56
  • 4
    One more thing the problem can also arise due to activity in background and framework killing process due to low memory and hence `onCreate()` getting called again! – Muhammad Babar Dec 01 '14 at 09:58
  • 1
    In Samsung Core 2, I needed to add the screenSize to configChanges like @WeiJiang says, otherwise it won't work! Tx for the help – Juliatzin Jan 15 '15 at 17:52
  • I wasted five hours only to know that I had kept the option of recreating activities on in my Developer Settings! – Skynet Mar 05 '15 at 11:34
  • After spending hours of trying to figure out why my objects created in onActivityResult always were null in other methods... Thank you so much! – user1419999 May 27 '15 at 09:52
  • Thank you sooo much... I spent weeks on this.... I asked this question myself without knowing what the solution was (I now have a link to this question in my answer): http://stackoverflow.com/questions/31425145/android-lollipop-issue-cannot-load-image-from-camera-to-imageview/32332020#32332020 – Cookienator Sep 01 '15 at 12:48
  • Should this not be a bug registered with Android that an activity is being created twice in parallel? I had two different valid copies of the same object in memory because of the race condition caused by this. –  Oct 09 '15 at 16:29
  • @Skynet : the application *must* work even if the setting is enabled. Killing an activity when starting the camera happens on a lot of low/mid-end devices. – Marc Plano-Lesay Jan 27 '16 at 15:56
  • This is not a solution at all, merely a work-around. The activity could be killed for a lot of reasons (the camera app using too much RAM being the first I can think of). – Marc Plano-Lesay Jan 27 '16 at 15:57
  • As @Kernald said, this solution is really a workaround. The correct way is to store the image in the model layer of the application, and assign to the ImageView when the Activity are created. – Jose Ramon Garcia Mar 19 '16 at 11:47
  • @Jose Ramon Garcia can you elaborate on your comment. I do not understand how to "store the image...". In my case the activity is destroyed even though I set my manifest file as stated above, and when my activity restarts, most of the time the onActivityResult is not called when the fragment is recreated, so I do not know the path to the image selected – Zvi Jun 24 '16 at 21:31
  • @Zvi you can use onSaveInstanceState and onRestoreInstanceState methods to read and write parcelables with your domain object, where you store the image. – Jose Ramon Garcia Jun 27 '16 at 10:07
  • Still not cleared to me. onSaveInstanceState is being called when my the intent of the camera is called, but at that point I do not know the filename of the image since it is not selected yet! I only know the path to the folder. – Zvi Jun 28 '16 at 08:47
  • 2
    @Akram this didn't solve the problem for me. my activity extends `AppCompactActivity` and has a `viewpager` with several `fragments`. one of the fragments is launching an image picker activity which is Gallery. after picking image or pressing back button, my activity is restarted even after putting all those configurations in `AndroidManifest.xml` – Edijae Crusar Oct 25 '16 at 14:09
1

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.

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62
1

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

Zvi
  • 2,354
  • 2
  • 26
  • 37
1

Disable "Don't keep activities" Developer setting. Otherwise it will destroy the activity you are leaving.

phnmnn
  • 12,813
  • 11
  • 47
  • 64