Activity overview

Activities or Activity are the fundamental part of Android platform. Activities are used to create User Interface for application by which user can interact with App. From starting of app to the end of app, user navigate within the app, clicking buttons or pressing back, all these things are done by activity. Activity is very similar to the pages of websites or window of desktop application. Android app can contain more than one activities means more than one screens.

Activity Lifecycle

Every activity in android has its individual lifecycle though which it passes when user interact with app. Lifecycle has several states (Callback Methods) which occurs one after another as per user interaction.

To navigate between states of activity android provides following callback methods (events) (image reference: android.com):

  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onDestroy()
  7. onRestart()

onCreate()

Every Apps must have to implement onCreate() callback. This is a first callback and fired when activity creates. On this callback you should do all the startup operations which should be executed when activity creates such as binding list, initializing class variables, defining an XML layout file etc... This callback fires only once in whole lifecycle of activity.

Example:

public class ActivityLifeCycle extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_activity_life_cycle);

    }

}

The example shows onCreate() mehod which defining XML layout file passing by R.layout.activity_activity_life_cycle to setContentView().

onStart()

This callback fires when Activity becomes visible to user and user can interact with App. This callback completes quickly and switch to OnResume() callback and does not stay in OnStart() callback.

onResume()

This callback fires when user start interacting with activity. It means the activity is at the foreground of your mobile screen. Activity stays in this state until another App comes to foreground and hide the running App and when this occurs, the Resumed activity enters into Pause state by calling OnPause() callback. When the activity return back to foreground, it will call OnResume() callback again.

onPause()

This callback fires when other app hides current running activity. That means activity is not destroyed yet, but activity is pause for some time until it comes to foreground. In this state user can’t able to instruct with activity because activity is not visible to user and activity also will not execute any code.

onStop()

This callback fires when activity is no longer visible to user. onStop() callback occur when activity has finished  running and any new activity has launched to screen. From this state Activity either comes back to interact with user or the activity finishes running. When activity comes back, system invokes onRestart() callback and if activity finishes running, system calls onDestroy() callback.

onDestroy()

This callback fires when activity destroy by the system. The system may also call this method when orientation change occurs and then system immediately call onCreate() to recreate the process in new orientation. This call releases all the resources.

onRestart()

This callback fires when activity has been stopped and started again. This callback always followed by onStart().

Example:

The example will help you to understand Android activity lifecycle. Just follow steps and use Android Studio to create application.

  1. Create new project (application) with name AndroidActivityLifeCycle.
  2. Modified the MainActivity.java file and add below code to it. You can find it to app/java/com.example.beinghuman.androidactivitylifecycle folder.
  3. Launch the emulator and run project.

I have used Toast to display running callback method.

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    /* Called when activity first created */

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toast.makeText(MainActivity.this, "The onCreate() event called", Toast.LENGTH_SHORT).show();

    }

    /* Called when the activity becomes visible to user. */

    @Override

    protected void onStart() {

        super.onStart();

        Toast.makeText(MainActivity.this, "The onStart() event called", Toast.LENGTH_SHORT).show();

    }

    /* Called when user start interacting with activity. */

    @Override

    protected void onResume() {

        super.onResume();

        Toast.makeText(MainActivity.this, "The onResume() event called", Toast.LENGTH_SHORT).show();

    }

    /* Called when another activity took focus. */

    @Override

    protected void onPause() {

        super.onPause();

        Toast.makeText(MainActivity.this, "The onPause() event called", Toast.LENGTH_SHORT).show();

    }

    /* Called when the activity is no longer visible to user. */

    @Override

    protected void onStop() {

        super.onStop();

        Toast.makeText(MainActivity.this, "The onStop() event called", Toast.LENGTH_SHORT).show();

    }

    /* Called just before the activity destroyed. */

    @Override

    public void onDestroy() {

        super.onDestroy();

        Toast.makeText(MainActivity.this, "The onDestroy() event called", Toast.LENGTH_SHORT).show();

    }

    @Override

    public void onRestart() {

        super.onRestart();

        Toast.makeText(MainActivity.this, "The onRestart() event called", Toast.LENGTH_SHORT).show();

    }

}

When you run the App, You will see Toast displaying message in sequence:

// The onCreate() event called

// The onStart() event called

// The onResume() event called

Now click on Back button  of emulator. You will see Toast displaying message in sequence:

// The onPause() event called

// The onStop() event called

// The onDestroy() event called

Now again open the app and click on Home button  of emulator. Your app will close and minimized to overview list. Now open your app from overview list by clicking Overview button . You will see Toast displaying message in sequence:

// The onRestart() event called

// The onStart() event called

// The onResume() event called