Activity And Activity Lifecycle
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Activity And Activity Lifecycle

In last module we saw the role of activity in “Hello world” example now we will deal with activity and its properties in depth. an activity is a window or a single screen   that contains the user interface of your applications just like frames in java. An application can have zero or more activities.

Generally, applications contain one or more activities, and the goal of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, These stages are known as activity’s life cycle.

Life cycle of an Activity plays a major role in ensuring that your application is working correctly or not.

Apart from activities, there is another important and unique concept in Android that is an intent.

An intent is basically the “glue” that enables different activities from different applications to work together seamlessly, ensuring that tasks can be performed as though they all belong to one single application.


An activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity has provided a window in which user can draw UI designing. Typically window fills the screen, but it may be smaller than the screen and could be smaller than the screen and can float on top of other windows.

By the help of an activity, you can place all your UI components or widgets in a single screen.

You might have worked with C, C++ or Java programming language and in that you must have seen that your program is starting from main() function. Android system is also very much similar to that, It initiates its program within an Activity starting with a callback method i.e. onCreate() method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity.

To create an activity, first of all you need to create a subclass of Activity or an existing subclass of it (Java class that extends the Activity base class):. In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed.

Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes.

To understand activity in a better manner we will see activity life cycle.

Activity  lifecycle

Activity lifecycle of an android application is controlled by 7 methods of class. The android Activity is the subclass of ContextThemeWrapper class.

There are 7 methods of Activity lifecycle which describes how activity will behave at different states.

Let us discuss each state (method) of an activity through an example -


package com.example.helloworld;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";
   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState) {
      Log.d(msg, "The onCreate() event");

   /** Called when the activity is about to become visible. */
   protected void onStart() {
      Log.d(msg, "The onStart() event");

   /** Called when the activity has become visible. */
   protected void onResume() {
      Log.d(msg, "The onResume() event");

   /** Called when another activity is taking focus. */
   protected void onPause() {
      Log.d(msg, "The onPause() event");

   /** Called when the activity is no longer visible. */
   protected void onStop() {
      Log.d(msg, "The onStop() event");

   /** Called just before the activity is destroyed. */
   public void onDestroy() {
      Log.d(msg, "The onDestroy() event");

An activity class loads all the UI component using the XML file available in res/layout folder of the project. Following statement loads UI components from res/layout/activity_main.xml file:


An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in yourAndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:


<manifest xmlns:android=""
   android:versionName="1.0" >
      android:targetSdkVersion="22" />
       android:theme="@style/AppTheme" >
          android:label="@string/title_activity_main" >
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>

The Activity base class defines a series of events that governs the life cycle of an activity. The Activity class defines the following events:

onCreate() — Called when the activity is first created.

onStart() — Called when the activity becomes visible to the user.

onResume() — Called when the activity starts interacting with the user.

onPause() — Called when the current activity is being paused (i.e. not visible to the user) and the previous activity is being resumed.

onStop() — Called when the activity is no longer visible to the user.onRestart() — Called when the activity has been stopped and is restarting again or its prior to start.

onDestroy() — Called before the activity is destroyed by the system (either manually or bythe system to conserve memory).


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



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

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

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

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

Now click on the lifecycleactivity icon.

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

If you see the emulator, application is started again.

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.



← Preview

Understanding Anatomy/ Internal Details of an Application

Next →

Applying Themes And Styles To Activity


Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.


  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App