0% found this document useful (0 votes)
340 views8 pages

Android Activity Lifecycle - Javatpoint

The document discusses the Android activity lifecycle, which describes the 7 different states an activity passes through. These include onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy. It provides details on when each method is called during transitions between states, such as when the activity becomes visible or invisible to the user. An example is also given demonstrating logging the method calls to the logcat output.

Uploaded by

Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
340 views8 pages

Android Activity Lifecycle - Javatpoint

The document discusses the Android activity lifecycle, which describes the 7 different states an activity passes through. These include onCreate, onStart, onResume, onPause, onStop, onRestart, and onDestroy. It provides details on when each method is called during transitions between states, such as when the activity becomes visible or invisible to the user. An example is also given demonstrating logging the method calls to the logcat output.

Uploaded by

Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/3/2016

AndroidActivityLifecyclejavatpoint

ContentMenu

AndroidActivityLifecycle
Android

Activity

controlled

by

android.app.Activity

Lifecycle

is

methods

of

class.

The

android Activity is the subclass of


ContextThemeWrapperclass.
An activity is the single screen in
android. It is like window or frame
ofJava.
By the help of activity, you can
place all your UI components or
widgetsinasinglescreen.
The 7 lifecycle method of Activity describes how activity will behave
atdifferentstates.

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 see on the logcat: onCreate, onStart and onResume methods


areinvoked.

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

Now see on the logcat: onRestart, onStart and onResume methods


areinvoked.

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

The onCreate() and onDestroy() methods are called only


oncethroughouttheactivitylifecycle.

prev

next

Share 116

http://www.javatpoint.com/androidlifecycleofactivity

8/8

You might also like