Android-Service - onDestroy () method, which is not called when using Force Stop

When I stop a service using the stop button on the Start Services tab, the onDestroy () method is called.

But when I force stop the application, onDestroy () is never called.

Any explanations about this?

or maybe a solution to start onDestroy () when stopping when stopping?

Thanks!

+7
source share
4 answers

When your power stops the application, this is what happens - it is the Force stopped. No warning, no callbacks, just stopped. The whole process is killed, and not one of the running components (Activities, Services, etc.) receives any warnings.

There is absolutely no guarantee that onDestroy() will be called. Move any critical application code to onPause() , which is called in most cases.

+3
source

From the documentation :

Once an action is created, onPause() is the last method that must be called before the process can be killed ... onStop() and onDestroy() can not be called. Therefore, you should use onPause() to write the most important persistent data (e.g. editing users) for storage.

To repeat this point, Force Stop is not designed to grace and exit the application with caution. If you have critical code that should be run every time the application terminates, you need to run it in onPause() .

+3
source

When the application receives a forced stop, Process.killProcess () is called, but not the onDestroy () function. Follow this link. You will get an idea. The Power of Android Stop Callback for an Application?

+1
source

I assume that you have code that you want to execute in onDestroy() , referencing your line:

"or perhaps a solution to run onDestroy () when stopped when stopped?"

Service method public void onTaskRemoved (Intent rootIntent) - this is what you are looking for, it will be called when the application is forcibly stopped.

0
source

All Articles