Android Activities and LifeCycle Methods

Tushar Tambi
AndroidDevelopers
Published in
8 min readAug 2, 2020

First Of All, If you do not have any prior knowledge of Android Development then you can refer it here.

Also, If you want to learn about the basic of android app components then you should refer here:

I have discussed the basics of activity here, so in this article, my major focus will be on lifecycle and call-back methods. Then we will see an example in which we will implement all these methods together.

So, I assume that by now you have a basic knowledge of android app development and Android App Components. So, let’s start with the Activities and their lifecycles.

As I always say, Android Apps can be written using Kotlin, Java and C++ languages. So, you can write them in any language that you want. This tutorial is based on Java.

Firstly, Let’s see what really activities are.

Here we will not talk about the standard definition of anything rather we will understand them by some example and in very simple language.

Below diagram is the overview of what we are going to learn in this article:

Activities:

Basically, activity is one screen that you see on your android app.

More clearly an activity provides a place where the app shows you a user interface.

This is not necessary that an app only contains one activity, an app can contain more than one activity. These different activities are put together to form a complete app.

If you have worked in any language like C, C++, Java then you remember that our program starts with a main() function. Here, android initiates code in a particular activity by invoking special call back methods. These call-back methods are corresponding to a particular stage of the app.

Don’t worry we will talk about call back methods very soon in this article.

Also, the point to note down here is although in an Android App there are many activities there is a minimum dependency of one activity over others. Each Activity can perform their tasks independently and can call other activities.

In fact, an activity can call other app’s activities also. For example, if we click on any link shared on WhatsApp with us it navigates us to our browser.

Now, as we have more than one activity in our app then how the android would know which activity will be the first activity when we open our app?

For this, we have to declare an intent filter(link) in our manifest file as a launching activity. Typically, one activity acts as a launcher activity and then from there we can navigate to any activity that we want.

For any activity that you create, you must declare them in the manifest file.

if you want to learn more about intent filters and how to configure the manifest you should refer here.

Activities callback methods can help to make your app error-free and can avoid crashing of the application. By handling transition make your app more robust and performant.

Activities lifecycle methods:

There are basically a set of six call-back methods that we use in android app:

1. onCreate()

2. onStart()

3. onResume()

4. onPause()

5. onStop()

6. onDestroy()

This diagram shows a visual representation of the call-back methods.

Now let's talk about Callback methods:

  1. onCreate():

This is a method that must be present in any activity. If you use C, C++ or Java language then you must know that our program execution starts with main() function. Here onCreate() plays the same role as of main() function.

When your system first creates an activity, onCreate() is the first function that gets called by the system.

In this method, you perform the basic operations like setting up the variable or our instances.

This method receives a parameter savedInstanceState, which is a Bundle object containing the activity’s previously saved state. If the activity has never existed before, the value of the Bundle object is null.

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); setContentView(R.layout.YOUR_XML_LALYOUT);
}

Here, setContentView(Layout) sets the layout for your activity. Instead of passing an XML layout you can use View and use it inside this method.

Now we must think that why this onCreate() method is enough for our work. This is because our activity need not be always in a created state. Suppose the user gets a phone call in between then this activity gets paused and after he received call this activity gets resume. So, we must have some other methods so that we can avoid breaking of our application.

2. onStart():

Whenever your activity is in the started state, the system calls this method.

Now, what the heck is this started state?

This is a state where activity gets visible to the user and the user can now interact with the application.

protected void onStart(){       Toast toast = Toast.makeText(getApplicationContext(),"onStart                 Called",Toast.LENGTH_LONG).show();
}

Our application does not stay in this callback method for long.

3. onResume():

Similar to previous methods this method is invoked when activity enters in the resumed state. Typically This is called when the user starts interacting with the application.

Our app is in this state until something interrupts our application like while using app user receives a phone call.

This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Typically, the onResume() callback restarts any paused UI updates, threads, or processes required by the activity but suspended when it was inactive

It is invoked when the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, with a user interacting with it. Always followed by onPause() when the activity goes into the background or is closed by the user.

protected void onResume(){     Toast toast = Toast.makeText(getApplicationContext(),"onResume                 Called",Toast.LENGTH_LONG).show();
}

4. onPause():

This method is invoked when the activity is running in the background but not killed completely yet. Typically it is counter to onResume().

You can use this method to release system resources used in this activity that may affect the device battery life.

Also, you should avoid heavy processing in this method.

Remember you must use onStop() if you want to kill that activity completely.

protected void onPause(){     Toast toast = Toast.makeText(getApplicationContext(),"onPause                 Called",Toast.LENGTH_LONG).show();
}

5. onStop():

This method is invoked when your app enters in a stopped state.

The stopped state is when your activity is no longer visible to the user, for example, you are invoking some new activity and it covers the full screen.

From the Stopped state, the activity either comes back to interact with the user, or the activity is finished running and goes away.

protected void onStop(){       Toast toast = Toast.makeText(getApplicationContext(),"onStop                 Called",Toast.LENGTH_LONG).show();
}

6. onDestroy():

The final call received before the activity is destroyed. This can happen

1. because the activity is finishing (when finish() is invoked) or

2. because the system is temporarily destroying this instance of the activity to save space.

To distinguish between these scenarios, check it with isFinishing() method.

protected void onDestroy(){     Toast toast = Toast.makeText(getApplicationContext(),"onDestroy                 Called",Toast.LENGTH_LONG).show();
}

7. onRestart():

It is invoked after the activity has been stopped and prior to its starting stage.

protected void onRestart(){    Toast toast = Toast.makeText(getApplicationContext(),"onRestart                 Called",Toast.LENGTH_LONG).show();}

Example:

MainActivity.java

package com.example.TestingApp;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
String msg = "Android : ";

// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}

// Called when the activity is about to become visible.
@Override
protected void onStart() {
super.onStart();
Log.d(msg, "The onStart() event");
}

// Called when the activity has become visible.
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
}

// Called when another activity is taking focus.
@Override
protected void onPause() {
super.onPause();
Log.d(msg, "The onPause() event");
}

// Called when the activity is no longer visible.
@Override
protected void onStop() {
super.onStop();
Log.d(msg, "The onStop() event");
}

// Called just before the activity is destroyed.
@Override
public void onDestroy() {
super.onDestroy();
Log.d(msg, "The onDestroy() event");
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.TestingApp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Now try to run your app and then let’s see what happen in the LogCat.

08-02 10:10:07.682 4480-4480/com.example.TestingApp D/Android :: The onCreate() event
08-02 10:10:07.683 4480-4480/com.example.TestingApp D/Android :: The onStart() event
08-02 10:10:07.685 4480-4480/com.example.TestingApp D/Android :: The onResume() event

Let us try to click-lock screen button on the Android emulator and it will generate following events messages in LogCat window in the android studio:

08-02 10:12:53.230 4480-4480/com.example.TestingApp D/Android :: The onPause() event
08-02 10:12:53.294 4480-4480/com.example.TestingApp D/Android :: The onStop() event

Let us again try to unlock your screen on the Android emulator and it will generate following events messages in LogCat window in Android studio:

08-23 10:14:41.390 4480-4480/com.example.TestingApp D/Android :: The onStart() event
08-23 10:14:41.392 4480-4480/com.example.TestingApp D/Android :: The onResume() event

Next, let us again try to click Back button on the Android emulator and it will generate following events messages in LogCat window in Android studio and this completes the Activity Life Cycle for an Android Application.

08-02 10:18:24.806 4480-4480/com.example.TestingApp D/Android :: The onPause() event
08-02 10:18:25.668 4480-4480/com.example.TestingApp D/Android :: The onStop() event
08-02 10:18:25.669 4480-4480/com.example.TestingApp D/Android :: The onDestroy() event

Important Links:

Download Java: https://www.java.com/en/download/

Java Tutorials: https://www.tutorialspoint.com/java/index.htm

Download Android studio: https://developer.android.com/studio

Learn more about Android Studio: https://developer.android.com/studio/intro

Official Android Developer Page: https://developer.android.com/index.html

Google Developers Codelabs provide a guided, tutorial, hands-on coding experience: https://codelabs.developers.google.com/?cat=Android

Links for Contact:

GitHub: https://github.com/8426988382

Email: Tushar Tambi

References:

If You have any doubts feel free to ask in comments.

“Any comments or suggestions are welcome”

--

--