Reputation: 1291
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?
Upvotes: 49
Views: 26249
Reputation: 841
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.
Upvotes: 84
Reputation: 6931
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.
Upvotes: 2
Reputation: 3592
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!
Upvotes: 2
Reputation: 908
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);
}
Upvotes: 46
Reputation: 853
Be sure you don't have the "Don't keep activities" Developer setting on, as it will destroy the activity you are leaving.
Upvotes: 29
Reputation: 152917
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()
.
Upvotes: 13