• Have Any Question?
  • contact@eitworld.com

Activity life cycle





The following diagram shows the activity life cycle: activity-life-cycle
In the life cycle of any activity, there are several callback methods are used to manage and develop a strong and flexible application. The life cycle of an activity is affected by the associations with other activities. Its task and back stack. The state of activity life cycle:
Active/Running: This state tells that the activity is in the foreground on the screen means it is the top of the stack.
Paused: This state tells that when another activity is in foreground and has focus but this is still visible. When another activity is visible on the top of this one and that activity is partially transparent means does not cover the full screen. A paused activity is completely alive means Activity object is retained in memory, here it maintains all the states of life cycle and maintains the information and also remains attached to the window manager. But in the case of less memory it can be killed by the system.
Stopped: When any activity is wrapped by other activity; means now the activity is in the background, it is stopped. In android, it also retains all the state and the member information. However it is not visible to the user. Hence window is hidden and when anywhere memory is needed then this is killed by the system.
Created and resumed: The android system has states either paused or stopped the activity. The android system can reallocate the memory by asking it to finish the activity. Whenever it shows the activity to the user then it must resume and restoring the previous state of the activity.
There can be three loops in the activity:

  • The overall lifetime of the activity takes place between the first call to the onCreate() method and when it destroy using the onDestroy(). Any activity can perform all the setup and consume all the resources when the onCreate() calls, and when onDestroy() method is used to destroy the activity and it releases the consumed resources. For example, when we download any file from the internet then the downloading is being in background and this is started to call the onCreated() method to start the download. When the download is finished then the thread is destroyed to call the onDestroy().
  • When any activity starts then the overall visibility of the activity takes place between the onStart() and the onStop() methods. During this time, activity is shown on the screen and user is able to see the activity on the screen. During this session of onCreate() and onDestroy(), we can maintain all the resources which are needed to display and run the activity. The both methods can be called again and again by the android system whenever activity becomes visible and become hidden to the user.
  • There are several chance when activities in the foreground. The foreground lifetime of any activity is start form the onResume() method and it scope ends when system calls the onPause() method. At this time, the activity is in the front of all the activities and it is visible to the user.

The methods used in the life cycle of android activity are following which describes the working of these callbacks methods in the overall life cycle of activity, including that when the android system kills the activity after the callback methods finished.
onCreate(): This method is called when the activity is create first time and this is where we should do all the normal static setup such as creating views, binding data to lists etc. This method passed a Bundle object that contains the previous state of the activity; if that state was captured. This method is always followed by the onStart() method.
onRestart(): This method is called after the activity has been stopped. It is used to just start the activity again. This method is always followed by the onStart() method.
onStart(): This method is called before the activity becomes the visible to the user. It is followed by the onResume() if the activity is in the foreground; or it is followed by onStop() if the activity becomes hidden.
onResume(): This method is called before the activity starts the interacting with the user. Here at this point, the activity is take place at the top of the activity stack and also has the input given by the user. It is always followed by the onPause() method.
onPause(): This method is called when the android system is about to start the resuming the another activity means we want to start the another activity then this method resume that activity and starts it. Mostly this method is used to save the changes to the persistent data and it is also used to stop the animation and resources that may be consumed by the CPU. It performs fast action because the next activity will not be resumed until it returns.
onStop(): This method is called when the activity is not visible for long time. This may happen because it is being destroyed or it may be happen because another activity is being resumed and wrapping it. When the activity is coming back to visible to the user then it is always followed by onRessume(); if it is being destroyed then it is always followed by the onDestroy() method.
onDestroy(): This method is called before the activity is destroyed. This is the last or final call that is received by any activity. This method is called when the activity is finishing; or it can be called when the system destroy the object temporarily to save the space.

Viewing methods of Android Activity

The activity life cycle uses the following these methods:

public class Activity extends ApplicationContext { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); }

These all methods can be overridden and we can provide the custom code for all the methods. In Android, all the activities are initialized by the onCreate() method and onPause() method can be used for the cleanup. Whenever we are implementing these methods then we have to call the super class otherwise we will get compile time error.



Real Time Web Analytics