简体   繁体   中英

Is there a method inside android to know when the system is going to kill my app?

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?

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().

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 (eg, "Terminate Application" toolbar button in "Android Monitor" tool window).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM