17

Possible Duplicate:
Difference between onCreate() and onStart()?
Android Activity Life Cycle - What are all these methods for?

What is the difference between OnCreate and OnStart?

My understanding is that OnCreate is only called the very first time the application is opened and is never called again. Is this true? Can someone please elaborate in their own words rather than copying and pasting a definition? Thank you!

Community
  • 1
  • 1
Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38
  • http://developer.android.com/reference/android/app/Activity.html – sissonb Sep 17 '12 at 23:53
  • http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – asteri Sep 17 '12 at 23:54
  • "My understanding is OnCreate is only called the very first time the **application** is opened". - Don't confuse the terms 'app', 'application' and `Activity`. An application can still run even when an `Activity` is destroyed and, after destruction, an `Activity` will be 'created' again and hence `onCreate(...)` will be called again regardless of whether other application components are still running or not. – Squonk Sep 18 '12 at 00:03

5 Answers5

24

As long as your device does not kill the activity, for example due to low system resources, then any time you leave your app and go back, onStart is called. If however the application process is killed, then when you return onCreate will be called again, because all of your resources will have been released.

DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
  • 3
    Not technically true, When you say "lose focus", this can happen when a popup (dialog) appears. The `Activity` will still be visible and will simply be paused rather than stopped. When the popup is closed, the underlying `Activity` will be resumed. – Squonk Sep 17 '12 at 23:56
17

The documentation seriously--I'm not kidding--has a very, very pretty image that describes how the Activity lifecycle (how it starts, works, and finishes) operates.

The image, directly linked here, basically says this:

  • onCreate() is called when the Activity is created; that is, it is launched or started. (Additionally, this triggers when the orientation is changed.) It is called again when the process is killed, then returned to.
  • onStart() is called following onCreate() at startup. Additionally, it is also called when the app is navigated back to after onStop() (and following onRestart()), which occurs after the Activity is no longer visible (which is during the time that "the user can see the activity on-screen, though it may not be in the foreground and interacting with the user").

The diagram honestly explains it better than I can in less than 1,000 words.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Awesome! Thank you so much Eric! One more question for you though; Do I need; setContentView(R.layout.activity); in the OnStart? Or is that redundant since I have it on my OnCreate already? – Josh Beckwith Sep 17 '12 at 23:58
  • @JoshBeckwith You only need it when creating the layout. That can safely be handled only in `onCreate()`. – Cat Sep 18 '12 at 00:00
4

This is perhaps best explained by starting with the opposite functions: onDestroy and onStop.

onDestroy is called when an activity is disposed of or shut down. The next time it's opened, onCreate is called.

onStop is called whenever an application goes out of view, and is no longer visible. This is usually caused by a new activity being created over the top of the old one. When the activity becomes visible again, onStart is called.

The diagram on this page does a really good job of explaining the various states an activity can be in, along with the relevant transition methods: http://developer.android.com/training/basics/activity-lifecycle/starting.html

David Underwood
  • 4,908
  • 1
  • 19
  • 25
1

Almost. onCreate is usually called when the app starts, but may also be called if the Activity is destroyed/released for other reasons. Basically, if you want to do one time set-up for the Activity, you should override onCreate. If you want to do something each time the user navigates to the Activity you should override onStart. If you want to do something each time the Activity becomes visible/active, use onResume. If you want to do one-time set up for the whole app, i.e. only runs once EVER, you should use onCreate, but check to see if you've done that set up, and only do it if you have not.

Check out the very useful diagram on this page: Activity reference

Joe K
  • 18,204
  • 2
  • 36
  • 58
0

This website provides a solid graphical representation of the Android lifecycle: http://developer.android.com/training/basics/activity-lifecycle/starting.html

onStart() is called whenever the application becomes visible. This includes when the application is first created and when it is brought back on the screen without being terminated. This second behavior occurs when a user switches applications and the app is sitting in the background. When the application becomes invisible, onStop() is called, but when the application is completely "destroyed", onDestroy() is called. After an onDestroy() call, the application must be created again at the beginning of the lifecycle.

It is important to note that onStart() is not called after the application simply loses focus due to something like a dialog. In such situations, onPause() is called followed by onResume() when focus is regained

Jack Satriano
  • 1,999
  • 1
  • 13
  • 17