Activity

An activity is the single,focused screen in android with user interface. We can place our UI with setContentView(View). which is XML file located in res/layout Folder. 

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity — the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

The entire lifecycle of an activity is defined by the following Activity methods.

  1. 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.  Always followed by onStart().
  2. onStart(): Called when the activity is becoming visible to the user.
  3. onResume():Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack.
  4. onPause():This method is called in 2 scenarios – when an activity is sent to background, when activity is killed by pressing back button.This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. 
  5. onStop():Called when the activity is no longer visible to the user.This may happen either because a new activity is being started or this one is being destroyed.
  6. onDestroy():The final call you receive before your activity is destroyed.This can happen either because the activity is finishing (by calling finish()) or because the system is temporarily destroying this instance of the activity to save space.
  7. onRestart(): Called after your activity has been stopped, prior to it being started again.

Starting an Activity

  1. The startActivity(Intent) method is used to start a new activity.
  1. The startActivityForResult(Intent, int)  where sometimes you want to get a result back from an activity when it ends.  The result will come back through your onActivityResult(int, int, Intent) method.
  1. void startActivityForResult (Intent intent, int requestCode,Bundle options)

void onActivityResult (int requestCode,int resultCode,Intent data)

Eg: public class MyActivity extends Activity {

....

     static final int PICK_CONTACT_REQUEST = 0; public boolean onKeyDown(int keyCode, KeyEvent event) {          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {              // When the user center presses, let them pick a contact.              startActivityForResult(      new Intent(Intent.ACTION_PICK,                  new Uri(“content://contacts”)),                  PICK_CONTACT_REQUEST);             return true;          }          return false;      }


     protected void onActivityResult(int requestCode, int resultCode,


             Intent data) {


         if (requestCode == PICK_CONTACT_REQUEST) {


             if (resultCode == RESULT_OK) {


                 // A contact was picked.  Here we will just display it


                 // to the user.

                 startActivity(new Intent(Intent.ACTION_VIEW, data));

             }

         }

     }

 }

Leave a comment