Java Reference
In-Depth Information
•
onCreate(Bundle)
iscalledwhentheactivityisfirstcreated.Thismethod
is used to create the activity's user interface, create background threads as
needed, and perform other global initialization.
onCreate()
is passed an
android.os.Bundle
object containing the activity's previous state, when
thatstatewascaptured;otherwise,thenullreferenceispassed.Androidalways
calls the
onStart()
method after calling
onCreate(Bundle)
.
•
onStart()
is called just before the activity becomes visible to the user.
Android calls the
onResume()
method after calling
onStart()
when the
activity comes to the foreground, and calls the
onStop()
method after
onStart()
when the activity becomes hidden.
•
onRestart()
is called after the activity has been stopped, just prior to it
being started again. Android always calls
onStart()
after calling
onRe-
start()
.
•
onResume()
iscalledjustbeforetheactivitystartsinteractingwiththeuser.
Atthispointtheactivityhasthefocusanduserinputisdirectedtotheactivity.
Androidalwayscallsthe
onPause()
methodaftercalling
onResume()
,but
only when the activity must be paused.
•
onPause()
iscalledwhenAndroidisabouttoresumeanotheractivity.This
methodistypicallyusedtopersistunsavedchanges,stopanimationsthatmight
beconsumingprocessorcycles,andsoon.Itshouldperformitsjobquickly,be-
causethenextactivitywon'tberesumeduntilitreturns.Androidcalls
onRe-
sume()
aftercalling
onPause()
whentheactivitystartsinteractingwiththe
user, and calls
onStop()
when the activity becomes invisible to the user.
•
onStop()
is called when the activity is no longer visible to the user. This
mayhappenbecausetheactivityisbeingdestroyed,orbecauseanotheractivity
(eitheranexistingoneoranewone)hasbeenresumedandiscoveringtheactiv-
ity.Android calls
onRestart()
after calling
onStop()
,when the activity
iscomingbacktointeractwiththeuser,andcallsthe
onDestroy()
method
when the activity is going away.
•
onDestroy()
is called before the activity is destroyed, unless memory is
tight and Android is forced to kill the activity's process. In this scenario,
onDestroy()
isnevercalled.If
onDestroy()
iscalled,itwillbethefinal
call that the activity ever receives.
Note
Android can kill the process hosting the activity at any time after
onPause()
,
onStop()
,or
onDestroy()
returns.Anactivityisinakillablestate