Question

I have been wondering for some time about the exact role of onStart() function in android life cycle. Most of the resources on net just say - it is called just before your activity becomes visible on the screen.

But the applications that I have made so far I have never used onStart(). I do all my initialization in onCreate() itself.

All other states in android life cycle have some or the other significance but the role of onStart() is not very clear.

Are there any specific things that must be done in onStart()? Is onStart() really required in life cycle since all the initialization can be done in onCreate() and it is also called before the activity becomes visible.

Can anyone help me out to understand onStart() clearly. Thanks in advance :)

Was it helpful?

Solution

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. After this, if you press Back into your activity B, B will be paused(onPause()), stopped (onStop()) and destroyed(onDestroy()), and activity A will be restored from back stack, started (onStart()) and resumed(onResume()). As you can see, system will not call onCreate() for A again.

How to use onStart()? For example, you should unregister listeners for GPS, sensors, etc in onStop() and register again in onStart(). If you register it in onCreate() and unregister in onDestroy(), then GPS service will work always and it will drain battery.

OTHER TIPS

Called when the activity is becoming visible to the user.

Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

More: http://developer.android.com/reference/android/app/Activity.html

what i personally do if i want to user onStart , i will assign my listeners (OnclickListener , ... etc ) in the onStart method .

i think really its usless as you could do every thing you want in the onCreate .

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top