As an android developer, if one does not know the application lifecycle of android application or does not have in-depth knowledge about it, there are very high chances that the application will not have a good user experience. Not having proper knowledge of the application lifecycle will not affect the working of that application but it will lead to poor user experience. Let’s take one example where the user is using the application, which takes a high amount of battery usage and on the other hand there is one another same application providing the same functionalities offered by the previous app, then there are very high chances that the user will shift to later application and will not be using the previous app. Having a lot of battery usage by the app can be the indication that the android developer of that app does not have proper knowledge of the application lifecycle.
This is an important topic and mandatory need to have proper knowledge of the processes in the android and application lifecycle and how different processes can affect the application state. The knowledge of the Application Lifecycle in Android is also a must because without having proper knowledge many times the process can be crashed while doing some important work which will ultimately lead to a bad impression of the app on the users. The lifecycle of the application’s process is not solely controlled by the application itself, but the application’s process lifetime depends on the various numbers of factors such as memory taken by the application, memory available in the system, how important is the process from the perspective of the user, how long it had been since the user not used that application.
Importance Of Having Knowledge of Process and Application Lifecycle
Knowing the Application lifecycle is necessary as though it will not affect the working of the app, but it will affect the user experience. Let say the user has two option in which user has the choice to choose one app of out two, in which both the application offers the same functionality, and involving the same type of task, but one app consumes more battery usage than the other, so, surely, the user will go for the application which consumes less battery. Thus developers need to understand the application is not holding resources and services when it is not in use, as it can be the cause of more battery usage. Thus developers should handle each application state of the android app and should release the resources when the app is in the background state.
LRU Cache Role in Optimization Of Android Applications
As it is known that a particular android device has limited storage capacity and processing speed, but since there are so many applications running on the user device, it becomes challenging for the android operating system to manage the resource properly so that none of the resources become a deficit of resource. So android operating system pushes the least used application in a cache called as LRU (Least Recently Used) Cache, if the application has not been used for the very long time, it will be pushed to the queue of the LRU and will be present at the front of the queue. If the application comes into use in the near time, it will be near to the back of the queue, and if the application is most recently used it will be present at the back of the queue. For example, if the user has not used Facebook for a very long time, then the Facebook application will be present at the front of the queue and if let’s suppose the user has used WhatsApp recently, then it will be present at the back of the LRU Cache queue. The LRU Cache Queue can be diagrammatically represented as:
The Priority of Processes In Android Application
To determine which process should be killed to proper memory and battery management, the Android operating system maintains a hierarchy in which all the processes are placed in order of their priority. The less priority is the process which can be killed first when the system wants to free up some resource. Android uses some set of rules and regulations to decide the priority of the processes based on the running state of the applications. Below are the process states that a process may have at any time in android applications. The priority of these processes decreases from top to down in order in which they are listed.
1. Foreground process
A foreground process is a process with which the user is currently interacting and using it. A process is considered to be in the foreground state if any of the below conditions hold:
- If the process is running an activity with which the user is interacting
- If it has a broadcast receiver which is currently in execution to receive any system update.
Example: Imagine the user is using Whatsapp, so the Whatsapp app will be said to be in the foreground state. This process is of the highest priority and they can only be killed by the system if the memory is so low that even this process cannot continue their execution.
2. Visible process
A visible process is a process when the activity can be visible to the user. The user does not directly interact with this process, as the activity corresponds to this process would be covered partially by another activity and the process will be in the onPause() lifecycle state. This process cannot be killed unless there is so much lack of memory in the system that the execution of these processes cannot be possible. Killing these processes will create a negative impact on user experience, as a user can see the activity corresponding to this process. These processes would be killed only when keeping them alive make it impossible for the foreground process to continue their execution.
Example: When some application needs permission like camera access, storage access, etc a prompt or dialog box will appear and ask for the required permission. So at this time, the process corresponding to the activity of the app which is running previously will go in the visible state.
3. Service Process
A process is said to be a service process if it is in running state and neither a foreground process and a visible process. These processes are not directly visible to the user of the application. This process is helpful for the applications which perform the background tasks such as background network data upload or download. The system will keep the service process alive until it becomes impossible for the system to keep the foreground process and visible process running.
Example: Uploading a PDF on the Whatsapp from the desktop is a service process that is done in the background.
4. Background process
A background state in which the onStop() lifecycle method of android is called by the system. Let’s suppose the user is using an app and suddenly presses the home button, so because of this action, the process goes from foreground state to background state. When the app goes from foreground state to background state, it goes to the LRU cache queue and will be placed in the front of the queue.when the user returns to that app, the process will return from background state to foreground state. So having knowledge of the process and application lifecycle in android along with how processes can decide the lifecycle time of the application is a must for an android developer which can lead to good user experience.
Battery Usage Of Phone When Using the Same Type Of Applications
From the above image, it can be said that applications that offer the same type of services lead to near about the same amount of battery consumption. (Consider the usage time of application to be equal) From the above image, it can be seen that as Youtube and Google meets are both Video Streaming and Linkedin and Whatsapp are both the same kinds of messaging app, though a little bit different, but these apps use the same kind of data-transport and services, thus consumes the same amount of battery.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!