Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Activity getting Destroyed after calling Camera Intent

I am having two Activities (A1 , 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. But the Problem arises once i click the save button (tick button in s3), my onActivityResult is not called instead A2's onDestroy method is called. I have few logics to be done in the onActivityResult fn.

I had read some post in Stackoverflow regarding this but i couldnt get useful output from that.I have my manifest like this for my second Activity(A2)

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

Note: In HTC One X my onActivityResult fn is getting called, but in my S3 second Activity(A2) is getting destroyed

Plz share ur thoughts on this. Thanks in Advance

like image 553
Sudarshan Avatar asked Apr 15 '13 12:04

Sudarshan


4 Answers

i had the same problem.i will be crazy but finally i have found a solution here. the issue is that when you click on "save" button of the camera the activity call change orientation method and it will destroy and recreated. try to set

android:configChanges="orientation|screenSize"

in android manifest (not only android:configChanges="orientation" because as suggest here, it not work for API level 13 or higher).

it prevent to destroy activity,it work for me.

like image 150
giacomo87 Avatar answered Nov 19 '22 05:11

giacomo87


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);
    }
like image 33
Rajesh Narwal Avatar answered Nov 19 '22 06:11

Rajesh Narwal


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

like image 27
Omaraf Avatar answered Nov 19 '22 06:11

Omaraf


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().

like image 13
laalto Avatar answered Nov 19 '22 05:11

laalto