Android Activity Lifecycle

Activity is the single screen like java frame or window, The Activity class provides  sequence of callback methods:             onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy().Each Callback Method invoke when Activity enters in new state.

Like main() method in C/C++ or Java Activity need onCreate() method for initiating its programming. As we discussed in above point that Activity Provides 7 callback methods but it doesn’t mean that you need to implement all callback methods.
Lets clarify these callback methods by below diagram:

android-activity-lifecycel

1.onCreate()         : This callback method called when the  activity is first created.

  1. onStart() : This method called when the activity becomes visible to user.
  2. onResume() : This method called When the user start interaction with App.
  3. onPause() : This method called when current activity is not visible to user and user is unable to send input and can not execute any code. In this method Current Activity will be on Pause condition and Previous Activity will be in OnResume() condition.
  4. onStop() : This callback method called when Activity is no longer visible to the user.
  5. onDestroy() : This method called before the activity destroyed by System. This method invoked if someone’s calling finish(), else system destroy temporarily the process to save space.
  6. onRestart() : This callback method called when the activity restart after stopping.

 

Lets Understand  Android Activity Lifecycle with the help of below Example. Follow the following steps for modifying the “Hello World” App. I hope you remember all the steps which we have done in “Hello World” Android Application, Here we have to do some modification for understanding Lifecycle of Activity.

Let’s run this modified code on AVD, We are done with coding. Follow the same steps as you have done in “Hello World” Application.

After Running your Application you need to check your LogCat. If you are unable to find logcat screen then just click alt+6 and check LogCat with “Android:” Message.