Thursday, July 28, 2011

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
  
package com.rdc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MyActivity extends Activity {
 private final static String TAG="In this method: ";
    
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
        Log.i(TAG,"Activity created");
    }
 
 @Override
    protected void onStart() {    
    super.onStart();    
    Toast.makeText(this, "onStart",Toast.LENGTH_SHORT).show();
    Log.i(TAG,"Activity started and visible to user");
    }
 
 @Override
    protected void onResume() {
    super.onResume();
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
    Log.i(TAG,"Activity interacting with user");
 }

    @Override
    protected void onPause() {
    super.onPause();     
     Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
     Log.i(TAG,"current activity got paused");
    }
    
    @Override
    protected void onStop() {    
    super.onStop();
    Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
    Log.i(TAG," current activity got stopped");
    }
    
    @Override
    protected void onRestart() {
    super.onRestart();
    Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
    Log.i(TAG,"activity again restarted");
    } 
    
    @Override
    protected void onDestroy() {    
    super.onDestroy();
    Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
    Log.i(TAG,"activity destored");
    } 
   
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Toast.makeText(getBaseContext(),"onSaveInstanceState..BUNDLING", 
      Toast.LENGTH_SHORT).show();
    Log.i(TAG,"activity data saved");
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Toast.makeText(getBaseContext(), "onRestoreInstanceState ..BUNDLING", 
      Toast.LENGTH_SHORT).show();
    Log.i(TAG,"activity previous saved data restored");
    }   
    
}

and main.xml is
  


    



the most important is AndroidManifest.xml
  


    

    
        
            
                
                
            
        

    


you can find the example zip file is here

cheers!!

I'd love to hear your thoughts! 

No comments:

Post a Comment