Tuesday, 9 July 2013

LIFECYCLE of an ACTIVITY (Part 2)

Hello friends,
            Welcome you all in the “LIFECYCLE of an Activity” part 2.

In the part1 we have learnt, what is an Activity and its Lifecycle?
           
In this part2, we create a lifecycle demo application, which demonstrate how the state of an Activity changes?
This application also demonstrates what are the system call invoke when an Activity changes the State.

Step1:-          Create an Android Application Project
                       
                        First of all create a new Project.
                        File à New à Android Application Project
                                    Application Name: LifeCycleDemo
                        Click Next
                        Click Next
                        Select BlankActivity, Click Next
                        Click Finish

Step2:-          Create User Interface

We are using the LinearLayout and vertical orientation for this applicaton.
 We need to set this in activity_main.xml file.

We are also putting some TextView and Button in our demo application to make it user interactive.

Modify the activity_main.xml with the following code:-

 

     


      

     

        

 



Step 3:-         Modify the strings.xml file

                        Please copy-paste the following code into the strings.xml file.
 
 

     LifeCycleDemo
     Hello world!
     Settings
     Start [ Sub_Activity ]
     Destroy Current Activity
     Status
     SubActivity

 

                       

Step 4:-         Defining color and fonts for the application.
           
                        Go to /res/values directory of the LifeCycleDemo project.
                        Create a new xml file and give name as color_and_font.xml
 
 
     #FFFFFF
     #000000

     #A8DFF4
     #D3E992
     #FFAFAF
     #FFECC0

     #0099CC
     #669900
     #CC0000
     #FF8A00

     44dp
     24dp
     10dp

 
                       
Step 5:-         Defining system calls, who changes the State of an Activity.

                        Please modify the MainActivity.java file with the following code:-
   
 package com.example.lifecycledemo;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.view.Menu;
 import android.view.View;
 import android.widget.TextView;

 public class MainActivity extends Activity
 {

  private TextView mStatusView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   mStatusView = (TextView) findViewById(R.id.status_current);
   mStatusView.setText("MainActivity: onCreate()\n");
  }
  protected void onStart() {
   super.onStart();
   mStatusView.append("MainActivity: onStart()\n");
  }
  protected void onResume() {
   super.onResume();
   mStatusView.append("MainActivity: onResume()\n");
   mStatusView.append("MainActivity: In RESUME state ---\n");
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.activity_main, menu);
   return true;
  }

  public void destroyActivity(View v) {
   MainActivity.this.finish();
  }



 }



                       

Step 6:-         Compile and RUN the LifeCycleDemo application.




This application shows you, when onCreate(), onStart(), onResume()  system call invoke and How the application came into RESUME (visible) state.





In the next part we will see, How the application move into the PAUSED, STOP and DESTROY state?