5

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 :)

ik024
  • 3,566
  • 7
  • 38
  • 61
  • I have already looked into that post but I am still not clear of what onStart() role is thats why I had to ask the question again to get a better insight about onStart() – ik024 Jan 23 '14 at 07:50

3 Answers3

11

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.

abielita
  • 13,147
  • 2
  • 17
  • 59
Dimmerg
  • 2,113
  • 1
  • 13
  • 14
  • I think its becoming clear. So the main role of onStart() is when the activity is called after being paused. If onCreate() is called then all the initializations should be done again, by using onStart() we can initialize those things which had been uninitialized in onstop(). So basically onStart() can be used to initialize those things which we will be uninitializing in onStop(). Thanks its clear now :) – ik024 Jan 23 '14 at 08:02
  • @user3064175 Not quite. Read the life cycle again, carefully. There are scenarios where the next callback after `onPause()` is `onCreate()`. – Simon Jan 23 '14 at 08:29
  • @Simon yes, you are right. Orientation change (and activity re-creation) can cause this scenario, for example – Dimmerg Jan 23 '14 at 08:41
  • oh yes, if the activity is in the onstop state and if the process is killed for some reason the activity comes back to onCreate which is followed by onstart. thanks Simon for reminding about it – ik024 Jan 23 '14 at 08:58
  • about the activities example, if Activity A uses a lot of memory, and Activity B tries to use more and more memory, wouldn't it cause Activity A to finish itself but first call the function onsaveinstancestate to save the state to be restored on the next onCreate of itself? about the GPS example, what would be the disadvantages of using onResume and onPause instead? – android developer Jan 23 '14 at 10:24
  • About first question: http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) - yes, it should be called. About second question - you can use onResume/onPause too, no problem. – Dimmerg Jan 23 '14 at 10:39
  • 2
    @user3064175 There are different ways of handling this, but my favourite is to assume that as soon as any activity goes to the background, it might be onCreate() called next. I save what I need to in onPause(), and bring it back in in onResume(). Only activity setup stuff (getting references to views, setting listeners etc) is done in onCreate(). This covers every scenario except a battery pull, but I'm not the only one that would have a problem with that one ;) – Simon Jan 23 '14 at 21:18
0

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

fida1989
  • 3,234
  • 1
  • 27
  • 30
0

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