Android application lifecycle


Application Lifecycle
              The phases that an application goes through from start to end is called application life-cycle.
It is very important to have a basic understanding of android application life-cycle to efficiently manage resources and ensure effective response to the users.
               An android application is a collection of activities and an activity correlates to a screen. Every android application runs inside its own instance of Dalvik Virtual Machine (DVM). At any point in time several parallel DVM instances can be active. Unlike in Windows or Unix process an Android application does not completely controls its application life-cycle.
               Android applications have limited control over their life-cycles. Every application should be prepared for untimely termination. Android gives priority to ensure smoother user experience by killing or terminating other processes. Applications are terminated based on its priority and state of the process. If two applications has the same priority, the process that has been at that priority longest will be killed first. But killed apps may be restored to their last state when requested by the user. Android devices are still considered to be resource constrained while comparing standards of modern computers and laptops. So its very important to ensure that the limited resources are managed effectively to make our applications to remain responsive all times.

Android Application priority
                The order in which the process are killed to reclaim resources is determined by the priority of their hosted applications. At any given time, android process can be in one of the below states.

Active process
                Active process are given highest level of priority. These are application components with which the user is interacting with. A process is said to be active, when any on
- It hosts an activity that the user is currently interacting
- It hosts a Service that's bound to the activity that the user is interacting
- It hosts a BroadcastReceiver that's executing its onReceive() method
- It hosts a Service executing either its onCreate(), onResume() or onStart() callbacks

Visible process
                As the name suggests visible activities are visible but they are not in the foreground or responding to the users. There are only very few visible process and they will be killed only under extreme circumstances.

Service process
               A process that is running a service and started with startService(). Services don't directly interact with the user, they receive lesser priority than the above two process. When the system terminates the running service it will attempt to restart them when resources become available. Playing music, downloading data in the network are some of the examples.

Background process
               Processes hosting activities that are not visible to the user, those process onstop() method has been executed. Usually there are many background process running, they are killed on last recently used(LRU) method to free up resources.

Empty process
                To improve overall performance, Android will often retain an application in memory after it has reached the end of its lifetime. Android maintain cache to improve the start-up time of the applications when they are relaunched. These process are routinely killed as required.            


Lifecycle Hierarchy events
                 The following are the hierarchical life-cycle events of android activity as indicated by indentation.

onCreate() - Called when the activity is created.
                 onStart()     -  Called to initiate the visible lifespan for the activity.  At anytime between onStart()                                          and onStop() the app may be visible. We can either be onResume’d or                                                        onStop’ped from this state.
                 onRestart() -   is called before onStart() if the application is transitioning from                                                                      onStop() to onStart() instead of being started from scratch
                          onResume() - Start of the foreground lifespan of the app, called when the activity will start                                                   interacting with the user. At this point your activity is at the top of the                                                             activity stack, with user input going to it
                           onPause() -   Here the app start losing its foreground state. We can either
                                                 be onResume’d or onStop’ped from this state
                 onStop() - The end of visible lifespan of the app. it is called when the app is no longer visible to                                     the user. This may happen either because a new activity is being started, an existing                                        one is being brought in front of this one, or this one is being destroyed.
onDestroy() - The final call you receive before your activity is destroyed

Hope this provides some info on Android application lifecycle. If you like to share us more, click here


 



Reactions:

1 comment :