Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onStop/onDestroy - when might these be used?

Tags:

android

Looking at the Activity Life Cycle diagram, I notice that onPause() and onStop() can both lead to the "process" being killed. This would require onCreate() to be called when the user wants to resume their application. The point being that onStop() is not necessarily called, nor is onDestroy(), but that onPause() may be the only event that the Activity may see. This being the case, onPause() must handle saving the application status so that the user can return back to it later, regardless of whether onStop() is called or not.

I can see onDestroy() being used to clean up Activity specific resources that would naturally be eliminated in a process kill action. Is there anything else that onDestroy() would be good for?

And what would onStop() be good for? Why would I want to override it for?

like image 555
user574771 Avatar asked Aug 29 '11 21:08

user574771


1 Answers

If I got your question right: It depends what you want to do with your application. Let's say you are programming application that uses GPS. In the onStop() which is called when the activity is no longer visible to the user, you can remove these requests. Or you can stop the some service if your application is running any. Or you can save preferences (not recommended, do it in onPause() instead), or you can close permanent connection to a server.....If I think of anything else, I'll add more...

like image 198
Nikola Despotoski Avatar answered Oct 12 '22 02:10

Nikola Despotoski