What is An Android Activity ?

Before explaining about Android Activity life Cycle, Let me give short introduction about Activities. An activity is a window that contains the user interface of your application. An application can have zero or more activities. Typically, applications have one or more activities and the main purpose of an activity is to interact with the user. In short, We can say each Activity represents a screen that an application can present to its users.

In an android app, there can be a number of windows, and you also need to switch between different windows or we can say that between different activities, For that purpose Intents are used. Activities interact with each other by using intents. They can also pass data from one activity to other using Intents.

Understanding The Android Activity Life Cycle

From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle. Understanding the life cycle of an activity is important to ensure that your application works correctly.

The Activity base class defines a series of events that govern the life cycle of an activity. The Activity class defines the following events for different stages of life cycle:

  • 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 and the previous activity is
    being resumed.
  • onStop() — Called when the activity is no longer visible to the user.
  • onDestroy() — Called before the activity is destroyed by the system (either manually or by
    the system to conserve memory).
  • onRestart() — Called when the activity has been stopped and is restarting again.

By default, the activity created for you contains the onCreate() event. Within this event handler is the code that helps to display the UI elements of your screen.

Following Figure shows the flow of different stages an Activity goes throw:

android activity life cycle

How To Use Different Methods

When an activity is started, the onStart() and onResume() methods are always called, regardless of
whether the activity is restored from the background or newly created. When an activity is created for
the first time, the onCreate() method is called.

Similarily, the onPause() method is called in two scenarios :—

  • When an activity is sent to the background,.
  • When it is killed when the user presses the Back button.

Following are the guidelines to use different methods :

  1. Use the onCreate() method to create and instantiate the objects that you will be using in your
    application.
  2. Use the onResume() method to start any services or code that needs to run while your activity is in
    the foreground.
  3. Use the onPause() method to stop any services or code that does not need to run when your
    activity is not in the foreground.
  4. Use the onDestroy() method to free up resources before your activity is destroyed.

I hope this article helped you to understand the android Activity Life Cycle easily.

Author

I am an core Android Developer with working knowledge of Kotlin and Java. And i also explore Flutter, React.js & Spring boot in extra time and having 8+ years of experience in this field. I have passion for solving complex problems. I love reading books and learning new challenging technologies in my extra time. Sharing my learning with others so that it can help others

Write A Comment