Activity LifeCycle source code for your testing Android example

onCreate(), onStart(), onResume(), onPause(), onSaveInstanceState(), onStop(), onRestart(), onDestroy() Android example source code.
AndroidLifeCycle.java

// license http://www.apache.org/licenses/LICENSE-2.0
public class AndroidLifeCycle extends Activity {
/*onCreate() called when the activity is starting. This is where most 
 * initialization should go: calling setContentView(int) to inflate the 
 * activity's UI, using findViewById(int) to programmatically interact 
 * with widgets in the UI, calling managedQuery(android.net.Uri, 
 * String[], String, String[], String) to retrieve cursors for data 
 * being displayed, etc. */    
 
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
Log.i("onCreate", "onCreate()");
 
   Button btnFinish = (Button)findViewById(R.id.idButtonFinish);
		btnFinish.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}// END onCreate
//////////////////////////////////////////
 
		/*onRestart() called after onStop() when the current 
		 * activity is being re-displayed 
		 * to the user (the user has navigated back to it). It will be 
		 * followed by onStart() and then onResume(). */
		@Override
		protected void onRestart() {
			super.onRestart();
			Toast.makeText(this, "onRestart()", Toast.LENGTH_LONG).show();
			Log.i("onRestart ", "onRestart()");
		}
 
/*onStart() called after onCreate(Bundle) — or after onRestart() when the activity had been 
 * stopped, but is now again being displayed to the user. It will be 
 * followed by onResume(). */    
 @Override
 protected void onStart() {
	 super.onStart();
	 Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
	 Log.i("onStart", "onStart()");
 }
 
/*onRestoreInstanceState(Bundle savedInstanceState)
 * This method is called after onStart() when the activity is 
 * being re-initialized from a previously saved state, given 
 * here in savedInstanceState. Most implementations will simply 
 * use onCreate(Bundle) to restore their state, but it is 
 * sometimes convenient to do it here after all of the 
 * initialization has been done or to allow subclasses 
 * to decide whether to use your default implementation. 
 * The default implementation of this method performs a restore 
 * of any view state that had previously 
 * been frozen by onSaveInstanceState(Bundle). */
 	     String sNewMyText = "";
	     int nNewMyInt = 0;
	     @Override
	     protected void onRestoreInstanceState(Bundle savedInstanceState) {
	    	 super.onRestoreInstanceState(savedInstanceState);
 
	    	 sNewMyText = savedInstanceState.getString("my_text");
	    	 nNewMyInt = savedInstanceState.getInt("my_int");
	    	 Toast.makeText(this, "onRestoreInstanceState()", Toast.LENGTH_LONG).show();
	    	 Log.i("onRestoreInstanceState", "onRestoreInstanceState()");
 
	     }
/*onResume() called after onRestoreInstanceState(Bundle), onRestart(), 
 * or onPause(), for your activity to start interacting with 
 * the user. This is a good place to begin animations, 
 * open exclusive-access devices (such as the camera), etc.  */
 @Override
 protected void onResume() {
  super.onResume();
  Toast.makeText(this, "onResume()", Toast.LENGTH_LONG).show();
Log.i("onResume", "onResume()");
 }
 
/*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().
 *The Activity can store preferences  SharedPreferences.Editor */
 @Override
 protected void onPause() {
	 super.onPause();
	 Toast.makeText(this, "onPause()", Toast.LENGTH_LONG).show();
	 Log.i("onPause", "onPause()");
 }
 
 /*onSaveInstanceState(Bundle outState) called to retrieve per-instance 
  * state from an activity before being killed so that the state can be 
  * restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) 
  * (the Bundle populated by this method will be passed to both). */
 
 String sMyText = "some text";
 int nMyInt = 10;
 
 @Override
 protected void onSaveInstanceState(Bundle outState) {
	 // Save away the original text, so we still have it if the activity
	 // needs to be killed while paused.
	 outState.putString("my_text", sMyText);
	 outState.putInt("my_int", nMyInt);
	 Toast.makeText(this, "onSaveInstanceState()", Toast.LENGTH_LONG).show();
	 Log.i("onSaveInstanceState", "onSaveInstanceState()");
 }
 
/*Called when you are no longer visible to the user. You will next 
 receive either onRestart(), onDestroy(), or nothing, depending on 
 later user activity.*/
 @Override
 protected void onStop() {
	 super.onStop();
	 Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
	 Log.i("onStop", "onStop()");
 }
 
 /*Perform any final cleanup before an 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.*/
 @Override
 protected void onDestroy() {
	 super.onDestroy();
	 Toast.makeText(this, "onDestroy()", Toast.LENGTH_LONG).show();
	 Log.i("onDestroy", "onDestroy()");
 }
 
 
 
}

Android Activity LifeCycle diagram

lifecycle-activity-android

This entry was posted in Android Examples Code and tagged , . Bookmark the permalink.


Comments are closed.



15,309 views