Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: when to use onStart(), onStop()?

Tags:

I've read several posts that describe the difference between onStart() and onResume(): onStart() is called when the activity becomes visible, onResume() is called when the activity is ready for interaction from the user. fine.

I've always just added code to onPause() and onResume(), and never bothered with onStart() and onStop().

Can anyone give some concrete examples of what you might do in onStart(), vs. onResume()? Same goes for onStop() and onPause(), how is onStop() useful? I must be missing something fundamental here.

like image 883
Jeffrey Blattman Avatar asked Mar 29 '12 22:03

Jeffrey Blattman


People also ask

What is the use of onStart in Android?

onStart(): This method is called when an activity becomes visible to the user and is called after onCreate. onResume(): It is called just before the user starts interacting with the application. onPause(): It is called when the app is partially visible to the user on the mobile screen.

What is the use of an onStop () function in Android?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user. This may happen for the following reasons, the user performing some action that invoked another activity to come to the foreground or the activity finished.

What is the difference between onPause () onStop () and onDestroy () lifecycle methods?

Difference between onPause(), onStop() and onDestroy() So, onPause() is logically before onStop(). From onPause() it is possible to call onResume() but it is not possible once onStop() is called. Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop().

What is onStart meant for?

onStart() is called when activity resumes from stopped state. For example, if you have activity A and starts activity B from it, then activity A will be paused ( onPause() ) and then stopped ( onStop() ) and moved to back stack.


1 Answers

onStop() will (for example) be called when you leave the activity for some other activity (edit: almost. see commonswares comment about dialog themed activities). For example if you use startActivity() in activity A to start activity B. When you press back in activity B you will return to activity A and onStart will be called.

This differs from some of the reasons onPause might be called without onStop being called. If for example the screen times out or you press the standy button onPause will be called, but probably not onStop (depending on memory available and whatnot), so it is a "lighter pause". onStop will be probably be called eventually even in this case, but not immediately.

Ok, but what's the use

Often there is no specific use, but there might be. Since your activities will keep its memory state on the stack even after you start some other activity, that stack will increase with the number of activities started (height of the stack). This can lead to large memory usage in some applications. After a while the framework will kick in and kill some activities on the stack, but this is rather blunt and will probably mean a lot of states to be retained when returning.

So an example use for onStart/onStop is if you want to release some state when leaving an activity for another and recreate it when you get back. I have used it to set listadapters to null, empty image caches and similar (in very specific applications). If you want to free the memory used by visible views in a listadapter you can recreate it in onstart and let the views be picked up by the gc. This will increase the likelyhood that the rest of the memory state of the activity will live on.

Some resources can be deemed good enough to save while the activity instance is alive and some only when it is on the front of the stack. It is up to you to decide what is best in your application and the granularity of create/start/resume gives you that.

like image 159
Sebastian Olsson Avatar answered Nov 09 '22 23:11

Sebastian Olsson