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 MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
String msg="activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(msg, "The Activity is being created");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(msg, "The Activity is started and visible to user");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(msg, "The Activity is Restarted after it was stopped");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(msg, "The Activity is stopped");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(msg, "The Activity is being resumed after it was paused");
    }
    @Override
    protected void onPause() {
        super.onPause();
     Log.d(msg, "The Activity is paused and another activity is running");
    }
    @Override
    protected void onDestroy() {
        super.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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error

Enjoy this blog? Please spread the word :)