简体   繁体   中英

Android development: My app is killed from time to time and restarted after that

I have an app with a service (onStartCommand return START_STICKY). All works fine, the app and the service are restarted if I swipe the app away or I reboot the device. This works.

But: The app is restarted from time to time (sometimes after minutes, sometimes after an hour). This means, the app is killed and restarted. But I don't know why.

  • Any hints?
  • Another process who kill my app?
  • Maybe an error in my code?
  • The OS?

Maybe you can give me a generally hint or a direction to search the error.

Thanks Hans

In general, you can never make an assumption that your service will not be killed.

You just have to design the app to resume your process with the state of it being killed by OS.

OS can kill your app whenever it thinks it unnecessary to keep it in the background. Your flag START_STICKY will get your service back to background, but this flag doesn't mean it will run forever without being terminated.

So make sure to use onSaveInstanceState() to keep your important states or data, or even SharedPreferences to keep the data in local storage just in case your app doesn't save states.

This link might help you: Android - Lifecycle and saving an Instance State questions

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