Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Android: Activity Life Cycle

Activity is the core of Android application.Each screen in application is an Activity.Every Application can have one or more activities.

Activities in the system are managed in an activity stack.When a new activity is launched it becomes on the top of the stack. Any previous activity will be below it and won’t come to the top until the new one exists.

The application on the top of the stack has the highest priority from the operating system. While the activity that is not visible has lower priority even if the a running activity requires more memory, the system can shut down that activity to free memory.

Android saves metadata (state) of each activity so that when a new activity launches so that it can come back to the activity when the user backtracks.

Life Cycle:



  • The activity starts, passes through onCreate(), onStart() the activity is still not visible to the user, onResume() then it comes to the foreground and becomes fully running.
  • If another activity launches or a notification appears the activity passes through the onPause() method. Then there would be two scenarios:
        1.if the system decides to kill your activity due to low memory the  
           activity starts the cycle again from onCreate() method with Bundle  
           savedInstanceState parameter that holds data about the previous  
           state of the activity.
        2.If the user resumes the activity by closing the new activity or the  
           notification the activity cycle continues from the onResume() method
  • When the user is about to close the activity the activity calls onStop()  method thenonDestroy() method when the system destroys the activity.
       But if another activity runs while the current one is was not shut, the   
       activity calles onStop()method and if it is not killed by the system it will 
       call onRestart() method then onStart()mehod and continues the cycle.

  • onCreate(): will be invoked in three cases:
       - the activity runs for the first time and it will be invoked with null  
         Bundle savedInstanceState parameter.
       - the activity has been running then stopped by the user or destroyed by  
         the system then it would be invoked with Bundle savedInstanceState  
         that holds the previous state of the activity.
       - the activity is running and you set the device to different resources like   
         Portrait vs landscape, then the activity will be recreated.in this method             
         you will create the user interface, bind data to controls and register the 
         event handlers for the controls. Then it is followed by onStart() method.

  • onStart(): will be invoked when the activity is first launched or brought back to the foreground it would be followed by onResume() if the activity continues and comes to foreground, or byonStop() if the activity is killed.
  • onRestart(): is invoked in case the activity has been stopped and is about to be run again. Always followed by onStart() mehod.
  • onResume(); invoked when the activity is about to come to the foreground and is on the top of the activity stack. It is the place where you can refresh the controls if the activity is using a service that displays some feeds or news. Always followed by onPause() method.
  • onPause(): is invoked when another activity launches while the current activity is launched or when the system decides to kill the activity. In this method you have to cancel everything you did in onResume() method like Stopping threads, stopping animations or clearing usage of resources(eg the camera).This method is followed by onResume() if the activity returns back to front or by onStop() if the activity is to be invisible.
  • onStop(): is invoked when a new activity is about to come over the current one or the current one is to be destroyed. Always followed by onResume() if the activity comes back or  onDestroy() if the activity is to be killed.
  • onDestroy():is invoked when the activity is shutting down because the activity called finish()[terminated the activity] or because the system needs memory so it decided to kill the activity.
Killable Methods:
           onPuase()
          onStop()
          onDestroy()

Summary:


  • The entire activity life cycle is between the onCreate() where you construct the UI and aquire resources and onDestroy() method where you release all resources.
  • The visible life time of the activity is between onStart() and onStop(). Between the activity is visible to the user although he may be unable to interact with it. Between the two methods you persist the state of the activity so that if another one comes to the foreground then comes back to the original activity you find the state persisted.
  • The foreground lifetime is between the onResume() and onPause(). During this time the activity is fully interactive with the user. The activity can go through the resume and pause states many times (if the device sleeps or a new activity launches) .

No comments:

Post a Comment