Android Activity Lifecycle - Javatpoint
Android Activity Lifecycle - Javatpoint
Android Activity Lifecycle - Javatpoint
AndroidActivityLifecyclejavatpoint
ContentMenu
AndroidActivityLifecycle
Android
Activity
controlled
by
android.app.Activity
Lifecycle
is
methods
of
class.
The
AndroidActivityLifecyclemethods
Let'sseethe7lifecyclemethodsofandroidactivity.
Method
Description
onCreate
calledwhenactivityisfirstcreated.
onStart
calledwhenactivityisbecomingvisibletotheuser.
onResume calledwhenactivitywillstartinteractingwiththeuser.
onPause
calledwhenactivityisnotvisibletotheuser.
onStop
calledwhenactivityisnolongervisibletotheuser.
onRestart
calledafteryouractivityisstopped,priortostart.
onDestroy calledbeforetheactivityisdestroyed.
http://www.javatpoint.com/androidlifecycleofactivity
1/8
7/3/2016
AndroidActivityLifecyclejavatpoint
AndroidActivityLifecycleExample
It provides the details about the invocation of life cycle methods of
activity.Inthisexample,wearedisplayingthecontentonthelogcat.
File:MainActivity.java
1. packagecom.example.activitylifecycle
2. importandroid.os.Bundle
3. importandroid.app.Activity
4. importandroid.util.Log
5. importandroid.view.Menu
6. publicclassMainActivityextendsActivity{
7. @Override
8. protectedvoidonCreate(BundlesavedInstanceState){
9. super.onCreate(savedInstanceState)
http://www.javatpoint.com/androidlifecycleofactivity
2/8
7/3/2016
AndroidActivityLifecyclejavatpoint
10. setContentView(R.layout.activity_main)
11. Log.d("lifecycle","onCreateinvoked")
12. }
13. @Override
14. protectedvoidonStart(){
15. super.onStart()
16. Log.d("lifecycle","onStartinvoked")
17. }
18. @Override
19. protectedvoidonResume(){
20. super.onResume()
21. Log.d("lifecycle","onResumeinvoked")
22. }
23. @Override
24. protectedvoidonPause(){
25. super.onPause()
26. Log.d("lifecycle","onPauseinvoked")
27. }
28. @Override
29. protectedvoidonStop(){
30. super.onStop()
31. Log.d("lifecycle","onStopinvoked")
32. }
33. @Override
34. protectedvoidonRestart(){
35. super.onRestart()
36. Log.d("lifecycle","onRestartinvoked")
37. }
38. @Override
39. protectedvoidonDestroy(){
40. super.onDestroy()
41. Log.d("lifecycle","onDestroyinvoked")
42. }
43. }
downloadthisexample
Output:
You will not see any output on the emulator or device. You need to
openlogcat.
http://www.javatpoint.com/androidlifecycleofactivity
3/8
7/3/2016
AndroidActivityLifecyclejavatpoint
Now click on the HOME Button. You will see onPause method is
invoked.
http://www.javatpoint.com/androidlifecycleofactivity
4/8
7/3/2016
AndroidActivityLifecyclejavatpoint
Afterawhile,youwillseeonStopmethodisinvoked.
Nowseeontheemulator.Itisonthehome.Nowclickonthecenter
buttontolaunchtheappagain.
Nowclickonthelifecycleactivityicon.
http://www.javatpoint.com/androidlifecycleofactivity
5/8
7/3/2016
AndroidActivityLifecyclejavatpoint
Ifyouseetheemulator,applicationisstartedagain.
http://www.javatpoint.com/androidlifecycleofactivity
6/8
7/3/2016
AndroidActivityLifecyclejavatpoint
Now click on the back button. Now you will see onPause methods is
invoked.
After a while, you will see onStop and onDestroy methods are
invoked.
http://www.javatpoint.com/androidlifecycleofactivity
7/8
7/3/2016
AndroidActivityLifecyclejavatpoint
prev
next
Share 116
http://www.javatpoint.com/androidlifecycleofactivity
8/8