Application Lifecycle in Android

Hello everyone. We will speak about the lifecycle in the Android Applications.

When I say lifecycle, what do I mean? The lifecycle starts from the moment we first started to open the application, up to the moment the application is opened, certain operations are carried out and closed. Also, it includes these two endpoints in the process. All these phases of the application, we call the life cycle. An application passes through certain phases from the first open until it closes. We say lifecycle to these phases.

Lifecycle has an important role in Android Application Development. Because when you develop an Android Application, you need to consider these lifecycles.

First, we will speak about what these lifecycles are and then we will speak about where and how we will use these lifecycles. Lifecycles have several stages. The first one is the application lifecycle, second is the activity lifecycle and the third one is the fragment lifecycle. We will speak about the application lifecycle in this article.

This is an Android Lifecycle Algorithm. You can see every lifecycle in this algorithm. When we first open the Android Application, OnCreate method works. This is the first stage for an Android Application. You should remember, we write most of the code in this method. Application is created in the OnCreate method using our java codes.  After OnCreate method, OnStart method works. After OnStart method, OnResume method works. That means when you open an application, these three methods work.

OnResume method continues as long as the application is running. But sometimes Android App can be thrown into the background or forced to pause. How this is happening. For example, when you work on an Android Application or when you play on an Android Game, a phone call or a message comes and your application or game is forced to pause. Because when a phone call comes, Android Device needs to show you this call. The time phone call comes, your Android Application is forced to pause. When the application is paused, OnPause method starts to work. After the call, when you continue your game, OnResume method starts to work again. This time just OnResume method works. Because the application is paused.

But when you first open the application, three methods worked. After the OnPause method, OnResume method starts to work. In this part, the application continues from where it paused.  Application pauses in the OnPause method.

When you go out of the application, OnStop method works. After OnStop method, if you want to open the application again, OnRestart method works this time. After the OnRestart method, OnStart and OnResume methods work.

So, when will the application pass to the OnDestroy method. After the application closed, it passes to the OnDestroy method. After the OnDestroy method, if you want to open the application again, respectively OnCreate, OnStart and OnResume methods work.

Also, there is another situation. When you jump from one application to another application, the first application goes to the OnPause and OnStop method. After a while, if you do not go back first application, Android Operating System closes this application itself.

But, before doing this, Android Operating System checks device memory. If all of the open applications force the operating system to work, it closes unused applications itself.

So, where we will use these lifecycles? For example, you play an Android Game. When you play a game, a phone call comes. When a phone call comes, your game application goes to the OnPause method. After the phone call, you need to continue where you stayed. To be able to continue where you left off, your scores, your stage, and other information need to be saved. We do these saving operations in the OnPause method.

Let’s think, after the OnPause method, your application goes to the OnStop method. Some times, some codes need to work in the OnStop method. Some applications should continue to run in the background. For example, you developed a GPS tracker app. This application is doing location tracking using the device’s GPS. When you put this application on the background, it should continue to track location. This application continues to run in the OnStop method.

Also, there is another thing. When your application on the background, device GPS will continue to work. And this runs out of device battery quickly. If your application consumes battery fast, people will not use your application. So, what can you do for this problem? For example, when the application goes OnStop method, you can close the device GPS. After the application comes back to the OnResume method, you can open GPS again. This will reduce battery usage.

But there are some applications need to work even OnDestroy method. For example, a mail or a message application. These types of applications need to check the server periodically.

Because of these explanations, you should know what the application is doing in its life cycles. If you know these processes, you can control your application better.

Let’s give another example. You develop a mail application. When the user writing an mail, a phone call comes. When the phone call comes, your application passes to the OnPause method. After the phone call, the user goes back to the mail application again. If you do not save in the OnPause method what the user wrote, the user must start writing from the beginning. Nobody wants that.

You need to consider all of these lifecycles when you develop an Android Application. 

So, this article was about Android Lifecycles. See you in the next article.

Written by Kadir Yapar

I am a software developer with a Master's degree in Computer Engineering and over five years of experience in the field. My expertise spans Android, iOS, and web development, and I have a track record of developing and publishing applications for these platforms. I am dedicated to creating high-quality, user-centric software solutions and continually stay updated with the latest industry trends. My ability to collaborate effectively in cross-functional teams and adapt to emerging technologies makes me a valuable asset in the software development landscape.


Leave a Reply

Your email address will not be published. Required fields are marked *