Hello, Everyone Today I am going to describe you about Activity Lifecycle of Android. Activity means nothing but it is simply a Screen or a page.When I first started programming for the Android I was quite confused about the life-cycle of activities. It seemed almost strange how the application started, stopped, and paused without seemingly any logical reasoning. The activity would be destroyed and re-created when the screen was rotated and paused when other applications were started. Now come to the point, seven methods are called during android activity lifecycle.
I will explain all this methods and also how they work and their usefulness in this post.
First of all see below Image for getting some idea about how activity lifecycle works.
1) onCreate ():- This method is called when the activity is first created. In this method you have to do all of your normal static set up: create views, bind data to lists, etc. Always followed by onStart ().
2) onStart ():-This method is called when the activity is becoming visible to the user. This is followed by onResume () if the activity comes to the foreground, or onStop () if it becomes hidden.
3) onResume():-This method is 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. This method is always followed by onPause().
4) onPause ():-This method is called when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A’s onPause () returns, so be sure to not do anything lengthy here.
5) onStop ():-This method is called when you are no longer visible to the user. You will next receive either onRestart (), onDestroy (), or nothing, depending on later user activity.
Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity’s process running after its onPause () method is called.
6) onRestart ():-This method is called after your activity has been stopped. It is called just before your activity start again. Always followed by onStart()
7) onDestroy():-The final call you receive before your activity is destroyed. This can happen either because the activity is finishing .someone called finish () on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing () method.
When the Activity first time loads the events are called as below:
When you click on Phone button the Activity goes to the background & below events are called:
i) onPause ()
ii) onStop ()
Exit the phone dialer & below events will be called:
When you click the back button OR try to finish () the activity the events are called as below:
Hope it helps in understanding Activity Lifecycle of android. Happy Coding 🙂