Android App Development— Understanding the Concept of an Activity

In building a native android application with Java, one of the major key concept to understand is working with components. An activity is an example of a component.

In this post i will explain the concept of an activity

An Activity by definition 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 is given a window to draw its user interface.
An activity also represents a single screen with a user interface just like window or frame, for example, a simple calculator would use one single activity. If you enhanced the calculator app to switch between a simple version and a scientific version, you would then use two activities.

If you have used C# or Java programming language then you must have seen that your program starts from main() method or function. This is very similar way, Android system initiates its program with in an Activity starting with a call on onCreate() callback method.

Every Android application runs inside its own process. Processes are started and stopped to run in an application and also can be killed to conserve memory and resources. Activities, in turn, are run inside the main UI thread of the application’s process. Once an activity is lunched, it goes through its life cycle, a term that refers to the steps the activity progresses through as the user (and OS) interacts with it. There are specific method callbacks that let you react to the changes during the activity lifecycle.

This callbacks are define in the Activity class. You don’t need to implement all the callbacks methods. However, it’s important that you understand each one and implement those that ensure your app behaves the way users expect.

  • onCreate() this is the first callback and its called when the activity is created for the very first time. Data could be initialize when this activity callback is called.
  • onStart() is called when the activity is becoming visible. This is an ideal place to write code that affects the UI of the application, such as an event that deals with user interaction. This callback is normally followed by onResume but could be followed by onStop if the activity becomes hidden.
  • onResume() is called when the activity is running in the foreground and the user can interact with it. It is followed by onPause.
  • onPause() is called when another activity comes to the foreground. The implementation needs to be quick, because the other activity cannot run until this method returns. The onPause callback is followed by onResume if the activity returns to the foreground, or by onStop if the activity becomes invisible.
  • onStop() is called when the activity is invisible to the user; either a new activity has started, an existing activity has resumed, or this activity is getting destroyed.
  • onRestart() is called when the activity is being restarted, as when the activity is returning to the foreground. It is always followed by onStart.
  • onDestroy() is called by the system before the activity is destroyed, either because the activity is finishing or because the system is reclaiming the memory the activity is using.

An example of a simple implementation of an activity class in an android application would look like this

//This code was copied from //
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");