Understanding the Application Lifecycle in Android
Before getting started, please hold on a minute and count the number of mobile applications present in your mobile phone. Tired of counting the number of Android apps on your phone? Yeah, this is common for all the Android users. We have applications for almost everything that we do in our daily life. But you are an Android developer and your way of looking on to the Android apps should be different from that of a regular Android user. You should know each and everything about the life of a particular Android app. You must know the Application life cycle of the app that you are making for your users. If you don’t have the proper understanding of the life cycle of your mobile application then no doubt, there will not be any effect on the working of the application but this will give a bad impression of your app on your users. Want to know how? Be with us.
In this blog, we will look upon the Android Application life cycle. We will try to find how this will affect the user experience. So, let’s get started.
Android Application life cycle
Android is a perfect example of true multi-tasking i.e. you can perform more than one tasks at a particular instant of time. Actually, we are not performing multiple tasks at a particular instant of time, rather it seems that we are performing more than one tasks at a time. In a very simple way, at any particular instant of time, only one app can be in the running state and other apps will be in the background state. All these processes are managed and handled in such a way that it seems that we are doing more than one task at some instant of time.
But the problem that arises here is that, a particular Android device has some limited amount of space and processing speed, and in order of have a fluent flow or to provide a fluent use of all the applications, to give a better experience to the users, Android pushes the application that is least used in some cache. By doing so, the app that is not being used for a longer period of time, will be pushed in the background and possibly it’s onStop() method will be called to stop the activity of that app.
LRU or Least Recently Used Cache is the cache used by the Android OS to push the applications that are least used in the nearer time. For example, if you are running the music app along with the Email, Facebook, Instagram, and Whatsapp application then the app which you have not used for a long time will be placed at the header or the front of the cache and the application that is used recently will be put in the back of the LRU cache queue.
For example, if the Email app is least used and the Facebook app is the most used app in the mobile phone at a particular time, then the Email app will be placed on the front of the queue of the LRU cache and the Facebook app will be placed at the back of the LRU cache queue.
Also, if the application is restarted or opened again, then it will be placed at the back of the LRU cache queue. For example, if you open the Email app again, then that Email app will be placed at the back of the LRU cache, not at the front of the LRU cache.
Priorities of Android Application
So far, we have seen that, in order to have a proper memory and battery management in the Android device, Android pushes or kills the applications that are having less priority. In order to free up some space from the Android device, Android uses some sets of rules and assign priorities to the applications based on the current running states of the applications. Following are the process status that are associated with the Android Application:
Note: Priorities are ranked in a higher to lower fashion
- Foreground process: A process is said to be in the foreground state if a user interacts with that process. For example, if you are watching some video on the YouTube app, then the YouTube app will be called to be in the foreground state because that app is currently being used by the user. So, the applications that are on the foreground have the highest priority.
- Visible process: A process is said to be in the visible state when the activity of the application can be visible but not in the fort. :) Confusing? Let’s have an example, whenever you are using some application that requires some kind of permission then you are using the visible process. For instance, let’s take the example of Instagram application, when you want to upload some images from your device then the app will ask you to give permission of the storage. At this time, your Instagram activity is visible but not in the foreground because, in the foreground, you are having the permission box asking for storage permission.
- Service process: A process is said to be a Service process if it is currently running but it does not come under the above two categories i.e. the foreground and the visible process. This is helpful for those applications that perform some background task such as downloading some data or uploading some data. One example of the service process is the uploading of files in Google drive where uploading of files is done in background.
- Background process: A process is said to be in background state if it’s onStop() method is being called by the Android. Suppose you are using some application and you suddenly press the home button of your mobile then at that time, your application will go to the background state from the foreground state. Also, the application will be placed in the LRU cache so that it will be called whenever the user reopens the app. This is done so because starting from scratch is very difficult as compared to start from an intermediate state.
- Empty process: A process is said to be in the empty state if it doesn’t come under the category of the above four mentioned process states. In an empty process, there is no active component of the application i.e. each and every component of the process will the in stop state. The application can be put in the LRU for better caching purpose but is memory is not present or low in amount, then that application will be removed from the cache also.
Why consider Application life cycle?
In the very beginning of the blog, I told you that if you don’t have the knowledge of the Application life cycle, then the working of the app will not be affected but the user’s experience will be bad. Hold on! So, why we have learned the Application life cycle if it doesn’t affect the working of our app?
Before answering the question, I would like to request you all to open the Power Usage of your mobile by the mobile applications present in your phone.
Here is mine:
You can see that apart from the Android system, at a particular instant of time, the Hotstar app which is a video streaming app used 12.1% of the total battery. Also, the YouTube app, which is again a video streaming app used 4.7% of the total battery available. This is because both these apps use some kind of data transport over the Internet and the battery is being used to establish, maintain and disconnect the service.
What if your app takes or uses a lot of battery? Will it be preferred by the users to use your app? The answer is no. While making an android application, you should handle each and every application state of the Android app.
For example, if you are using some data receiving and sending operation in your app and you want to manage and hold the session whenever the app is running then you should handle the operations that are to be done when your application will be in the background state. If your application is in the background state then you should not hold the session because this will result in more resource utilization and this, in turn, will use more battery and memory. So, in the onPause() state, you should release the session and in the onResume() state, you should again build and session and do the rest operations.
By doing so, your application will have a good impact on your users and your users will recommend the app to their friends. So, Android application life cycle is very important to make an Android application.
In this blog, we learned the Application life cycle in Android. We saw how different states can affect the working of Android apps on our phone. We also saw how we can make use of application life cycle to make an app that builds a great impression on the user and will help you to make an app that can be reached to billions of users.
Keep Learning :)