Pr.Pg Next Pg

Activity Life Cycle tutorials

 

Activity Life Cycle Methods

 

onCreate() :

  • Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().

 

onRestart() :

  • Called after your activity has been stopped, prior to it being started again. Always followed by onStart()

 

onStart() :

  • Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

 

onResume() :

  • 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. Always followed by onPause().

 

onPause ():

  • Called as part of the activity lifecycle 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.

 

onStop():

  • 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.

 

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.

 

Code Example

  • In our Hello Android application, when we click on Change Activity button, we start the new activity Example, however when we click on the Back button on our device we can still go back to our previous activity i.e MainActivity.

  • At times we would want our activity to terminate as soon as a new activity is started.

  • When we shift to the activity Example, the onPause() method is called upon for the MainActivity followed by the onStop() method.

  • In order to kill our activity we have to make changes in the onPause() method.

  • Following are the changes to be made in the MainActivity.java file

  • Right click between the braces of the MainActivity class.

  • Then Source > Override/Implement Methods.

  • Then select onPause() method from the list.

  • The method will then be added to the code.

  • Then give a call to the finish() method.

  • The code below shows the changes made in the MainActivity.java file to enable this killing of the activity.

 

 

 

 

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

finish();

 

}

 

  • The finish() method is used to when the activity is supposed to be closed.

  • Now when we run the application and click on Change Activity we will start the Example activity but the MainActivity will be closed and not run in the background.

  • We can check this by pressing the back button which will close the app rather than going to the previous screen.

 

Pr.Pg border                                              Next Pg