Android Activity Lifecycle

Email this to someonePrint this pageShare on Google+0Share on Facebook1Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.An activity is the single screen in android. It is like window or frame of Java.By the help of activity, you can place all your UI components or widgets in a single screen.The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

Let’s see the 7 lifecycle methods of android activity.

Method Description
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user.
onResume called when activity will start interacting with the user.
onPause called when activity is not visible to the user.
onStop called when activity is no longer visible to the user.
onRestart called after your activity is stopped, prior to start.
onDestroy called before the activity is destroyed.

Android-Activity-Lifecycle

Android Activity Lifecycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.

File: MainActivity.java

 


Output:

You will not see any output on the emulator or device. You need to open logcat.

Android Activity Lifecycle 1

Now see on the logcat: onCreate, onStart and onResume methods are invoked.

Android Activity Lifecycle 2

Now click on the HOME Button. You will see onPause method is invoked.

Android Activity Lifecycle 3

After a while, you will see onStop method is invoked.

Android Activity Lifecycle4

Now see on the emulator. It is on the home. Now click on the center button to launch the app again.

Android Activity Lifecycle5

Now click on the lifecycleactivity icon.

Android Activity Lifecycle6

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

Android Activity Lifecycle7

If you see the emulator, application is started again.

Android Activity Lifecycle8

Now click on the back button. Now you will see onPause methods is invoked.

Android Activity Lifecycle10

After a while, you will see onStop and onDestroy methods are invoked.

Android Activity Lifecycle11

The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

Email this to someonePrint this pageShare on Google+0Share on Facebook1Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

About Team

Browse Archived Articles by Team