1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Intent & Lifecycles in Android App Development

The Application Lifecycle

Contents

keyboard_tab

The course is part of this learning path

The Application Lifecycle
Overview
Difficulty
Intermediate
Duration
2h 15m
Students
4
Description

This course focuses on lifecycles and intent in Android and then moves on to look at services, receivers, and Android app binding.

Intended Audience

This course is intended for anyone who wants to learn how to start building their own apps on Android.

Prerequisites

To get the most out of this course, you should have some basic knowledge of the fundamentals of Android.

Resources

Course GitHub repo: https://github.com/OakAcademy/android-app-development-with-kotlin/tree/main/Section%207%20-%20Intent%20and%20Lifecycle/Intent 

 

Transcript

Application lifecycle. Hello everyone. We're going to talk a little bit about lifecycle in android applications. So, what do I mean when I say lifecycle? So, the lifecycle basically starts from the moment we first start to open the application. Up to the moment the application is open, certain operations are carried out and closed. It also includes these two end points in the process. And all these phases of the allocation are called the lifecycle. So, an application passes through certain phases, from the first open, until it closes and all of these phases are what's known as lifecycle. Now lifecycle has a very important role in android application development because when you develop an android application you will need to consider these lifecycles. So, first let's talk about, what these lifecycles are, and then we're going to talk about, well, pretty much where and how we're going to actually use these lifecycles. So, life cycles have different stages. First one is the application lifecycle, the second is the activity lifecycle, and the third one is the fragment lifecycle. So, in this particular video we're going to focus on the application lifecycle because we are developing android applications.

So, right here is an android lifecycle algorithm. You can see every lifecycle in this algorithm. So, when we first opened the android application, the onCreate method works and this is the first stage for an android application. And you should remember that we write most of the code in this method. Application is created in the onCreate method using Java code. After the onCreate method, the onStart method works. Then after the onStart method, onResume is the method that works. So, this means that when you open an application, these are the three methods that will work. The onResume method continues as long as the application is running. But sometimes android apps can be a little finicky, they can be thrown into the background, or forced to pause. How does that happen? Well, for example, when you, I don't know, when you work on an android application, or when you play an android game, let's say a phone call or message comes in and your application or game is forced to pause right? Now because when a phone call comes in, the android device needs to show you this call. The time the phone call comes in, your android application is forced to pause.

So, when the application is paused, the onPause method starts to work. After the call, when you continue your game, onResume method starts to work again. So, this time just the onResume method works because the application is paused. But when you first open the application, three methods are employed. After the onPause method, the onResume method starts to work. So, this is the part that the application continues from where it paused. The application pauses in the onPause method. Now when you go out of an application, the onStop method works. And then after the onStop method, if you want to open the application again, the onRestart method works this time. Then after the onRestart method, onStart, and onResume methods will work. So, when will the application pass to the onDestroy method? Well, that's after the application is closed, it passes to the onDestroy method. Then after the onDestroy method, if you want to open the application again, respectively onCreate, onStart, and then the onResume method will work. Now also, there is another situation, when you jump from one application to another application. The first application goes into the onPause and onStop method, and then after a while, if you do not go back to that first application android operating system will close the application itself.

But before it does this the android operating system checks the device memory. Now, if all of the open applications forced the operating system to work, it will close any unused applications itself. So, now it brings us to the question, where are we going to use these lifecycles? For example, you play an android game and when you play a game, a phone call comes in and when a phone call comes in your game application goes to the onPause method. Then after the phone call, you're going to need to continue where you left off. Right? So, to be able to continue where you left off, your scores, your stage and all the other information needs to be saved. And we do these saving operations in the onPause method. So, let's think about that for a second. After the pause method, your application goes to the stop method. Sometimes some codes need to work in the onStop method. So, some applications should continue to run in the background. For example, let's say that you developed a GPS tracker app. This application is doing location tracking using the device's GPS. When you put this application into the background, it should continue to track the location.

So, this application continues to run in the onStop method. Now also there's another thing, when your application runs in the background, device GPS will continue to work, and we all know what that does. It will drain the device battery pretty quickly. So, if your application consumes the battery fast people, they're not gonna like that very much, they're going to complain, and not going to use your application. So, what could we do about this problem? Well, for an example, let's say that when the application goes to the onStop method, you can close the device GPS. So, then after the application comes back to the onResume method, you can open the GPS again. That's how you reduce the battery usage. Okay, that's all well and good, but there are some applications that need to work even the onDestroy method. For example, a mail, or a message application. So, these types of applications need to check the server periodically. So, now that you know what's going on with these explanations, you should really know what the application is doing in its particular lifecycle. You should know these processes, so you can control your application better. So, let me give you another example. Let's say that you develop a mail application.

So, when the user is writing an email, a phone call comes in, and when the phone call comes in, your application passes to the onPause method, and then after the phone call, the user goes back to the mail app again. Now, if you don't save in the onPause method what the user wrote well, the user has got to start all over again from the beginning. Who wants that? That's frustrating. So, in other words, you need to consider all of these particular lifecycles when you develop an android app. So, this was the theoretical part. Okay. And it's only just a few of the variety of scenarios that you may encounter when developing your app. Right now, let's put the theory to the side, and let's go to android studio and see it in practice. Now you know that we have a couple of projects by now in the android studio. So I want to show you these particular methods on this project. So, here you can see the onCreate method. Now we have worked most of the time in this method, but it's really not the only method. I want to show you some of the other methods. There's an easy way to call methods in android studio. When you write the first letters of the method, android studio shows you the methods that start with these letters. So, I will call onDestroy method first, and then second onPause method, third onStop method, after that onStart, and onResume method. Now, which method did we figure? The last one is "onRestart" method. And when we run our application, we need to track which of these methods are currently working. How the heck do you do that? Well, I'll tell you, that's what I'm here for, I will teach you how to track application stages. When you develop an Android application, Android Studio keeps "Log Records". So, you can use these log records to track the application workflow. So, here I will put a log record, and it will take two parameters. The first one is "log name", and this shows me how I will track this log. The second one is "message". So, when I find this log it will show me this message and I'll write here "Log.d", now d means "debug" and I can see this log message in the debug logs. Now, I'll write a message for the tag and it will show me the debug logs with the message title. And second is the message, now write here "onCreate". Whichever method works, I want the name of the method to be written. With the "onCreate" method, I want to be written "onCreate". in the same way, for the "onDestroy" method, it will write "onDestroy". And then that way I can do the same for the rest. So, let's run the application and see which methods will work.

So, before we run it, I do need to show you where you see your log records. So, there's a "Logcat" section on the bottom of Android Studio. After you click the 'Logcat', I will select 'Debug' records, now because I write the "Log.d", it will track debug logs. Let's say if I write "Log.i", it will track "info" logs. So, I selected the debug records. So now, I can run the application. And what do you see here? So, when the application was first opened, the "onCreate", "onStart", and "onResume" methods work. So, we showed you this on the algorithm. Now, let's continue, so that when I put the application in the background, the "onPause" and the "onStop" methods work. So, let me put my application on the background again. Now, because I want to pass to another application, I will go back to my application again. When I go back to my application again, "onRestart", "onStart", and "onResume" methods worked. Okay, so good, let's look at the algorithm. So, you can see on the algorithm, after the "onStop" method, "onRestart", "onStart", and "onResume" methods all work. So, what if I closed my application using the 'back' button? So, after I click the 'back' button, "onPause", "onStop", and "onDestroy" method worked. So, what needs to be done at these stages? We will write into these methods. So, we will write in "onCreate" method; what we need to write in the onCreate method? When the application goes into the "onPause" method and if we need to save some information, we will write codes in the onPause method. And when the application goes into the "onStop" method, if we want our application to do something in the background, we can just write these codes in the onStop method. So for example, I'll continue on from the earlier example, if you want to close device GPS, when the application is in the background, I will write these codes in the "onStop" method. Now, I didn't want to get too serious in the beginning but I want to tell you that and I'll emphasize again, this topic is important. I'm going to finish up this video here but you will be able to continue to work on the device and see some of the other methods yourself. I do want to get started in the next video but review this if you need to. In the next video, we're going to continue this topic on the activity lifecycle. So, I'll see you then in the next video.

About the Author
Students
86
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics