1

I'm trying to resolve some mysterious behavior when my application gets killed when I'm in gallery.

I have such flow:

From Activity_1 I'm startActivityForResult Activity_2. From Activity_2 I'm letting user to pick an image from desired source.

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Hint", REQUEST_CODE_SELECT_PICTURE);

Sometimes while user is in the gallery (usually when there are lots of apps in the background running) my app gets killed. Without any warning whatsoever - my application process disappears from process list. And when user picks a photo he's able to return to Activity_2 but all previously selected images are lost.
If user presses "Next" at that point app should return to Activity_1

Intent data = new Intent();
data.putStringArrayListExtra(EXTRA_PHOTOS, new ArrayList<String>(selectedPictures));
setResult(RESULT_OK, data);
finish();

But it looks like there is no Activity_1 and app just restarts. No crash, just restart. And user gets back to Activity_1, but looses all data.

It is very hard to reproduce and is very annoying. Is there a way to prevent such behaviour?

Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
  • 2
    your app is killed because it is not in focus and your device is short on memory. you are supposed to save all the necessary data before you leave your app for that case. – njzk2 Dec 06 '13 at 14:06
  • Android's [Process Lifecycle](http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle) documentation has some details how it picks apps that get killed. – zapl Dec 06 '13 at 14:20

2 Answers2

3

Android kills applications when he needs ressources. So, in your case, the gallery needs more ressource and Android kills your application.

But your application is notified (via onPause, onDestory etc) when it's happening , you need to save your application state via onSaveInstanceState() and get it back together when your application returns

A. Binzxxxxxx
  • 2,812
  • 1
  • 21
  • 33
Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31
  • Is there a way to reproduce "resource starvation" manually? I mean with 100% reliability for easy testing? – Martynas Jurkus Dec 06 '13 at 14:36
  • 1
    Here similar question on SO : http://stackoverflow.com/questions/3656594/simulate-low-battery-low-memory-in-android – Plumillon Forge Dec 06 '13 at 15:56
  • Killing process or application did not work for me. It would kill the gallery app too as it is in same process. You should use `Immediately destroy activities` in [developer options](http://developer.android.com/tools/debugging/debugging-devtools.html). This simulates desirable conditions. – Martynas Jurkus Dec 09 '13 at 07:58
1

I can't be 100% sure, but this sounds normal for Android. If there are in fact a lot of apps running, Android will kill apps, if they're not in focus (the activity is not being displayed).

This should be handled by properly implementing the activities' lifecycle methods. You can save the state there and recreate it once Android creates a new instance of your activity. You can read about it here: http://developer.android.com/reference/android/app/Activity.html

pablochan
  • 5,625
  • 27
  • 42