My solution is to save the file uri in SharedPreferences and recover it in onResume.
I encountered the same situation: I have a photo list, and press one photo frame in the photo list will call android native camera app to take the picture. Sometimes (like 2% of usage) when coming back from android native camera app, the picture does not show in the very photo frame as expected. I was puzzled and couldn't figure out what happened. Until one of my colleages set "don't keep activities" in "developer options", and encountered the bug all the time, then I knew it's a problem of activity being killed.
Below are some code to demonstrate my solution:
public static class PhotoOnClickListener implements OnClickListener {
...
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.fileUri[index] = getOutputMediaFileUri(MEDIA_TYPE_IMAGE, "xxxxxx");
activity.saveKeyValue("game_photo_list_file_uri_" + index, activity.fileUri[index].toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, activity.fileUri[index]);
activity.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
...
}
private void tryRecoverFromBeingKilledOfLowMemory() {
String s;
for (int i = 0; i < fileUri.length; i++) {
s = readKey("game_photo_list_file_uri_" + i);
if (s != null) {
fileUri[i] = Uri.parse(s);
updatePhoto(i);
}
}
}
@Override
protected void onResume() {
super.onResume();
if (readKey("from_game_main") != null) {
removeKeysPrefixedBy("game_photo_list");
removeKey("from_game_main");
removeKey("uploader_id");
}
tryRecoverFromBeingKilledOfLowMemory();
}
In the code:
- readKey, saveKeyValue, removeKey, removeKeysPrefixedBy are inherited from a CommonActivity acting as common operation to SharedPrefeneces.
- The key from_game_main indicated that the current resume is normal and should start with empty photo list. The key from_game_main is saved just before startActivity in GameMainActivity. Other wise, current resume is a recovery from being killed by low memory.
Hope it helps.