Android Basics — Activity Lifecycle (The implementation)

Simran Sharma
4 min readFeb 18, 2021

So in the last story/article, we discussed basic app components and the activity lifecycle. If you haven’t read that yet, then I would recommend you to read that first as this is based on it.

Here is the link:

In this story/article, we’ll be using Android Studio 4.1.2.

So first let’s create a brand new Android Studio project.

  • Open Android Studio
Welcome screen on opening Android Studio
Welcome screen on opening Android Studio
  • Click on “Create New Project”.
  • Select “Empty Activity” and click “Next”.
  • You’ll see the “Configure Your Project” screen that would be looking somewhat similar to this:
Project details
Project configuration
  • Now fill in the details as per what you like and hit “Finish”.

Give it some time as it is building and create a workspace for your project. Android Studio will automatically generate all the files required for it to run. And this is how your screen would look like after all that has finished:

The default screen for a new project
The default screen for a new project

As you can see there are two tabs open in front of you:
MainActivity.java and activity_main.xml

The .java files will tell your app what to do and the .xml files will tell your app how to look like.

So let’s edit the MainActivity.java file and add the lifecycle methods: onCreate(Bundle savedInstanceState), onRestart, onStart(), onResume(), onPause(), onStop() and onDestroy() . Out of which the onCraete(Bundle savedInstanceState) is already provided by default.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onRestart() {
super.onRestart();
}

@Override
protected void onStart() {
super.onStart();
}

@Override
protected void onResume() {
super.onResume();
}

@Override
protected void onPause() {
super.onPause();
}

@Override
protected void onStop() {
super.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
}

Now let’s add the Log.d() lines to log which method is invoked and when. These log lines are a single-line output.

Log.d() takes two String parameters: a TAG and a Message. The TAG is the title of the logline and the Message is then the description for that title in that logline.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d("ActivityLifecycle", "onCreate()");
}

@Override
protected void onRestart() {
super.onRestart();

Log.d("ActivityLifecycle", "onRestart()");
}

@Override
protected void onStart() {
super.onStart();

Log.d("ActivityLifecycle", "onStart()");
}

@Override
protected void onResume() {
super.onResume();

Log.d("ActivityLifecycle", "onResume()");
}

@Override
protected void onPause() {
super.onPause();

Log.d("ActivityLifecycle", "onPause()");
}

@Override
protected void onStop() {
super.onStop();

Log.d("ActivityLifecycle", "onStop()");
}

@Override
protected void onDestroy() {
super.onDestroy();

Log.d("ActivityLifecycle", "onDestroy()");
}

Now run the program by clicking on the run button on top or by pressing the shortcut key “Shift+F10”. Now the app will start to build and then install and then launch. Once the program is up and running, now open the Logcat by pressing the shortcut key “Alt+6”. Now in the search box right on the top of the Logcat, there is a search button. Search the TAG that we gave (i.e. “ActivityLifecycle”).

Search box in Logact
Search option in the Logcat

When you search “ActivityLifecycle”, the logs will automatically appear like below:

Logs when you start the application
When you start the application

And when you press the back button in your app, these below log lines are to be seen:

Logs when the application is closed
When the application is closed.

So, as a conclusion, when you start an app first onCreate() will be invoked then onStart() and then the onResume() . And when you close an application first onPause() then the onStop() and then onDestroy() will be invoked.

Now what if we do not press the back button but instead press the recents/overview button which opens the recent applications and click and open the application once again, then this the Logcat image:

Restarting the application from the recents

As you can see the onDestroy() was not invoked in this case. The onDestroy() is only invoked in the case when the app is closed. Which in this case did not happen. One more change is that instead of onCreate() , the onRestart() was invoked.

So this is how you, yourself can check and experiment with the other methods as well. Don’t get limited with just the methods that I provided in today’s code. There are more Lifecycle methods that can be invoked and I only tried pressing the back button and pressing the recents button. There are so many more ways to experiment to find out which methods are called when.

--

--