4

I'm developing a phonegap application that uses the camera. In low memory situations, when the camera is launched, my application is killed by the system, sometimes without calling onDestroy() method (now I know that only onPause() is guaranteed).

I can override the onPause() method (in javascript or java) to store the app status, and recover it when the app is restarted. The problem is that the picture file uri is lost, and my application can't obtain it.

Do you know any way in Android for recalling to my callback function when the camera returns the picture uri, and my application has been killed? Any workaround?

I think that this problem is common to all android developments that uses startActivityForResult(), but I haven't found any solution.

Thanks in advance ;-)

Carlos Bravo
  • 101
  • 8

2 Answers2

0

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:

  1. readKey, saveKeyValue, removeKey, removeKeysPrefixedBy are inherited from a CommonActivity acting as common operation to SharedPrefeneces.
  2. 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.

happy15
  • 79
  • 1
  • 4
0

Right before startActivity, try setting a flag on the saved intent to surface it rather than killing and restarting it:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

See my post here: https://stackoverflow.com/a/29630548/2782404

Community
  • 1
  • 1
kwills
  • 116
  • 10