0

We have one very difficult to reproduce problem with an app. The use case is as follows:

  • User opens app
  • User leaves app in background
  • User openes 5 to 7 more apps
  • System kills our app
  • When user tries to resume app, app crashes due to NullPointerException

I was trying to use console log with Application class method onTrimMemory() and onLowMemory() but this methods are not being called. Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?

Jeka
  • 1,600
  • 3
  • 22
  • 36
  • Try to ovveride those methods in the Activities' code, rather than the Application's one – Massimo Feb 12 '16 at 14:10
  • Did you properly declare your Appliction class in your manifest? – malrok44 Feb 12 '16 at 14:18
  • As far as I know, this is not possible in a direct way (I would be happy to be disproved as I also had searched for such a way). In an indirect way, you could implement some kind of keep alive signal. Using it to either make your app appear still being needed (so other idling background apps will be killed before your app) or to check the timestamp of last keep alive signal to see that the app has been dead before reaching this check (resulting in recreating all necessary parts). – chevalier Feb 12 '16 at 14:30
  • It seems ok, but from what I've just read, this might not be a solution to your problem. Maybe this will give you an answer http://stackoverflow.com/questions/21040339/how-to-know-when-my-app-has-been-killed – malrok44 Feb 12 '16 at 14:34
  • 2
    "To save additional data about the activity state, you must override the onSaveInstanceState()" source: http://developer.android.com/training/basics/activity-lifecycle/recreating.html – clocksmith Feb 12 '16 at 14:37

2 Answers2

2

You can save whatever states you need in onSaveInstanceState() and restore them in onRestoreInstanceState(). onDestroy() is what the system will most often call to clear room while your Application is in the background, but it is not guaranteed to be called, so it is better to have already saved your states in onSaveInstanceState().

D. Pappas
  • 81
  • 5
0

Is there any method or callback I can be listening to know when android system will kill my app due to many more apps being opened and in that case for me to do something?

Not really. Depending on circumstances, onDestroy() of running activities and services will be called.

We have one very difficult to reproduce problem with an app

That should be reproducible in a matter of seconds:

  1. Run your app.

  2. Switch to another app.

  3. Kill your app's process from your IDE (e.g., "Terminate Application" toolbar button in "Android Monitor" tool window).

  4. Try returning to your app from the overview screen (a.k.a., recent-tasks list)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491