Activity Lifecycle

I’m just starting with Android App Development, and if you are a beginner like me, you probably want to understand two of main concepts of Android: Activities and Intents.
After Hello World Example i wanted to know Activity Life-cycle so i learn this and sharing with you..
This is the Android Activity Life Cycle Diagram described by Google

 As you can see in diagram there 7 method in activity base class for life cycle :
onCreate(Bundle savedInstanceState)
This method is Called when the activity is first created.

onStart()
This method is Called when activity is becoming visible to the user.

onResume()
This method is Called when the activity will start interacting with the user.

onPause()
This method is Called when the system is about to start resuming a previous activity.

onStop()
This method is Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one.
    
onRestart()
This method is Called after your activity has been stopped, prior to it being started again.

onDestroy()
This method is The final call you receive before your activity is destroyed.

Other Methods:
onSaveInstanceState(Bundle outState)
This method is called before an activity may be killed so that when
it comes back some time in the future it can restore its state.

onRestoreInstanceState(Bundle savedInstanceState)
This method is called after onStart() when the activity is being
re-initialised from a previously saved state.
The default implementation of this method performs a restore of any
view state that had previously been frozen by onSaveInstanceState(Bundle).

Create a simple android app with default activity "MyActivity" then put the code like this
  1.     
  2. package com.sunil;  
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.widget.Toast;  
  8.   
  9. public class MyActivity extends Activity {  
  10.  private final static String TAG="In this method: ";  
  11.       
  12.  @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Toast.makeText(this"onCreate", Toast.LENGTH_SHORT).show();  
  17.         Log.i(TAG,"Activity created");  
  18.     }  
  19.    
  20.  @Override  
  21.     protected void onStart() {      
  22.     super.onStart();      
  23.     Toast.makeText(this"onStart",Toast.LENGTH_SHORT).show();  
  24.     Log.i(TAG,"Activity started and visible to user");  
  25.     }  
  26.    
  27.  @Override  
  28.     protected void onResume() {  
  29.     super.onResume();  
  30.     Toast.makeText(this"onResume", Toast.LENGTH_SHORT).show();  
  31.     Log.i(TAG,"Activity interacting with user");  
  32.  }  
  33.   
  34.     @Override  
  35.     protected void onPause() {  
  36.     super.onPause();       
  37.      Toast.makeText(this"onPause", Toast.LENGTH_SHORT).show();  
  38.      Log.i(TAG,"current activity got paused");  
  39.     }  
  40.       
  41.     @Override  
  42.     protected void onStop() {      
  43.     super.onStop();  
  44.     Toast.makeText(this"onStop", Toast.LENGTH_SHORT).show();  
  45.     Log.i(TAG," current activity got stopped");  
  46.     }  
  47.       
  48.     @Override  
  49.     protected void onRestart() {  
  50.     super.onRestart();  
  51.     Toast.makeText(this"onRestart", Toast.LENGTH_SHORT).show();  
  52.     Log.i(TAG,"activity again restarted");  
  53.     }   
  54.       
  55.     @Override  
  56.     protected void onDestroy() {      
  57.     super.onDestroy();  
  58.     Toast.makeText(this"onDestroy", Toast.LENGTH_SHORT).show();  
  59.     Log.i(TAG,"activity destored");  
  60.     }   
  61.      
  62.     @Override  
  63.     protected void onSaveInstanceState(Bundle outState) {  
  64.     super.onSaveInstanceState(outState);  
  65.     Toast.makeText(getBaseContext(),"onSaveInstanceState..BUNDLING",   
  66.       Toast.LENGTH_SHORT).show();  
  67.     Log.i(TAG,"activity data saved");  
  68.     }  
  69.       
  70.     @Override  
  71.     protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  72.     super.onRestoreInstanceState(savedInstanceState);  
  73.     Toast.makeText(getBaseContext(), "onRestoreInstanceState ..BUNDLING",   
  74.       Toast.LENGTH_SHORT).show();  
  75.     Log.i(TAG,"activity previous saved data restored");  
  76.     }     
  77.       
  78. }  

and main.xml is
  1.     
  2.   
  3. <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">  
  4. <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Activity Life Cycle.." android:textsize="20sp">      
  5. </textview></linearlayout>  

the most important is AndroidManifest.xml
  1.     
  2.   
  3. <manifest android:versioncode="1" android:versionname="1.0" package="com.sunil" xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <uses-sdk android:minsdkversion="8">  
  5.   
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:label="@string/app_name" android:name=".MyActivity">  
  8.             <intent-filter>  
  9.                 <action android:name="android.intent.action.MAIN">  
  10.                 <category android:name="android.intent.category.LAUNCHER">  
  11.             </category></action></intent-filter>  
  12.         </activity>  
  13.   
  14.     </application>  
  15. </uses-sdk></manifest>  

you can find the example zip file is here

cheers!!

I'd love to hear your thoughts! 

2 comments:

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *