Android Studio Tutorial For Beginner #5: Understanding Activity Life-cycle

Creating activity and learning how it works is a very important part while you start to learn android app development, this may also be the first question asked from you while giving an interview for an android app developer position.

An activity represents a single screen with a user interface just like a window or frame of Java. Android activity is the subclass of ContextThemeWrapper class. This should be mentioned in the Manifest.xml file otherwise runtime error will occur.

The activity has different methods which you can call on different phases of activity.

Activity LifeCycle

onCreate() is the first method called whenever a particular activity is created.

onStart() is method called when activity is visible to the user.

onResume() is called when the user interacts with the activity. the activity is running and the user can perform some task on it.

onPause() is called when no user input or data manipulation is required from that activity, in this method, the current activity is paused and activity that was running before is resumed.

onStop() is called when activity is no longer visible to the user or has stopped.

onDestroy() is called before system destroy that activity

onRestart() is called after the activity has been stopped.

Let’s take a simple example to understand how your activity is working. Create a new Project write following code in your

import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
String msg="activity";
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(msg, "The Activity is being created");
    protected void onStart() {
        Log.d(msg, "The Activity is started and visible to user");
    protected void onRestart() {
        Log.d(msg, "The Activity is Restarted after it was stopped");
    protected void onStop() {
        Log.d(msg, "The Activity is stopped");
    protected void onResume() {
        Log.d(msg, "The Activity is being resumed after it was paused");
    protected void onPause() {
     Log.d(msg, "The Activity is paused and another activity is running");
    protected void onDestroy() {
        Log.d(msg, "The Activity is going to be destroyed by system");

Now run your application click on run icon, and select your device.

connect your device to PC via USB

You can see the results in your Logcat window. if you can not find Logcat than press ALT+6. in the search box, you can write ‘activity’ to get this below messages.

LogCat window

Now you have successfully understood the activity life cycle. Subscribe to my youtube channel or my newsletter to keep yourself updated.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments

Enjoy this blog? Please spread the word :)