When I stop a service using the stop button under the Running Services tab, the method onDestroy() is called.
But when I force stop the application, onDestroy() is never called.
Any explainations about this?
or maybe a solution to fire onDestroy() when force-stopped?
When your force stop an app, exactly that happens - It is Force Stopped. No warning, no callbacks, just stopped. The entire process is killed, and none of the running components (Activities, Services etc) are given any warning.
There is absolutely no guarantee that onDestroy() will be called. Move any application critical code into onPause(), which is called under most circumstances.
From the documentation:
Once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed... onStop() and onDestroy() might not be called. Therefore, you should use onPause() to write crucial persistent data (such as user edits) to storage.
To reiterate this point, Force Stop isn't intended to be graceful and exit the app in a caring manner. If you have critical code that must be run each time app finishes you need to run it in onPause().
When the application gets force stop, Process.killProcess() is called but not onDestroy() function. Go through this link. You will get some idea.
Android force Stop callback to application?
I am assuming you have code that you want to execute in onDestroy() referring to your line:
"or maybe a solution to fire onDestroy() when force-stopped?"
The Service method public void onTaskRemoved(Intent rootIntent) is what you are looking for, it will be called when the app is force-stopped.
I know it's an old question, but I was having the same issue and in my case I was using a binding service, so even after called stopSelf() Android does not call onDestroy() method, in order to force it I need to call unbindService() first
Related
I am working on a WebRtc Calling App. When my application is in background when incoming call come the Calling Activity PopsUp for call . And When call hung Up i need to send app to the recent background state.
So i came around a solution moveTaskToBack(), which is working perfectly but the problem is after calling Activity finish it shows the Last Activity for 2 or 3 seconds and then going back to recent app. Also tried in onStop() but the result remain the same .
#Override
protected void onDestroy() {
moveTaskToBack(true);
}
Is there a better Alternative of moveTaskToBack() without any Delay? If anyone need more clarification on question let me know in comments. Thanks in advance.
You shouldn't do it onDestroy, because it's not guaranteed that to be called in Activity Lifecycle.
In the onStop() method, the app should release almost all resources that aren't needed while the user is not using it. For example, if you registered a BroadcastReceiver in onStart() to listen for changes that might affect your UI, you can unregister the broadcast receiver in onStop(), as the user can no longer see the UI. It is also important that you use onStop() to release resources that might leak memory, because it is possible for the system to kill the process hosting your activity without calling the activity's final onDestroy() callback.
Problem might be the time takes to calling onDestroy. You can try to call it where you are finishing activity.
Through Android's Activities doc, it is said that the methods onStop() and onDestroy() are not guaranteed to be called.
[...] once the activity is created, onPause() is the last method that's guaranteed to be called before the process can be killed—if the system must recover memory in an emergency, then onStop() and onDestroy() might not be called [...]
I would like to know, when this situation occurs, is the app also killed within the activities or just the activity itself is killed?
Answer is app process is also get killed and can be recreated.
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
Please check http://www.vogella.com/tutorials/AndroidLifeCycle/article.html
Application with only stopped activities and without a service or executing receiver. Android keeps them in a least recent used (LRU) list and if requires terminates the one which was least used.
I am making an application for which I want to logout the user whenever he removes the app from recent used apps or the app no longer it exists in the background. How can I do that?
You should probably put your logout code in your onDestroy() method of all the activities you are using in your app.
Actually, to be safe, you should save things in onPause() because (at least pre-3.0) your process can be killed by the OS after it calls onPause(). This implies that onStop() and onDestroy() may or may not be called , but then it would make no sense to put your logout code in onPause() method.
In any case, there is no guarantee that onDestroy() will ever be called. If the OS decides to kill your process it won't bother calling onDestroy() on any activities.
But if there is any place you should put your logout code it should be onDestroy().
I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur? I'm especially interested in whether it's possible for a Service to be killed, yet its VM would continue to run.
I ask because I have a service that registers ContentObservers in the service's onStartCommand() method and unregisters them onDestroy(). If the service's onDestroy() method was never called because the whole VM was killed (along with the observers it created,) that would be fine. But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.
I'm aware that a Service's onDestroy() method may never be called but can someone tell me when such a scenario might occur?
Here are three off the top of my head:
If the user Force Stops you from the Settings app
If Android needs RAM in a hurry (e.g., to process an incoming phone call) and elects to terminate your process to free up that RAM
You terminate the process from DDMS
Also, if your service crashes with an unhandled exception somewhere, Android may consider the service to be defunct and skip onDestroy(). I'm not sure about this one, as I haven't specifically tried it.
But I'm wondering if it's possible for a service to "go away" without onDestroy() being called, while the observers it created would live on and continue to receive changes.
Other than the unhandled-exception possibility I mention above, I am reasonably certain that if the process will be terminated in the conditions where onDestroy() is not called.
Also if the app is reinstalled/updated , ondestroy() is never called.
If I have a service running in my Android phone and I shutdown, will the service call onDestroy? I want the service to perform a task just before the phone shuts down. Although the Google Droid guide says not to put such code in onDestroy, I do not know what else can be done.
If startService() is called to create your Service you should call stopService(), then onDestroy() will be called. If you use bindService() with BIND_AUTO_CREATE flag, than unbindService() caused onDestroy() automatically. What code they say shouldn't be put there? If it uses this Service of cause onDestroy() is not a good idea, if it's something like notification it's ok to put it there.