CS4521:   Mobile and Topics in Web Programming

Activity

  • presentation layer of an Android application, e.g. a screen which the user sees
  • An Android application can have several activities and it can be switched between them during runtime of the application
  • single, focused thing that the user can do
  • takes care of creating a window for user
  • presentation to the user
    • full-screen windows
    • floating windows
    • embedding inside of another activity

TIP: Later we will learn about another kind of presentatation layer that are like "mini-activities" --called Fragements. You can create an activity made up of one or more Fragements --- a way of partitioning your screen space for more complicated GUIs (like frames in XHTML). Using multiple Fragements to create an Acitivity is useful particularly when designing for the larger real-estate of a tablet (over a phone) and can be used also effectively to alter the GUI between a phone (show only Activities with one fragment or a few in it) to a tablet (so more complex Acitivities combinging multiple Fragments)

Activity Lifecycle

void onCreate(Bundle savedInstanceState) 
void onStart()  
void onRestart()  
void onResume()  
void onPause()  
void onStop()  
void onDestroy()

  • one time initialization stuff goes in onCreate
  • stuff you need to setup each time the Activity is about to run goes in onResume
  • resources like camera that you may use in Activity need to often be released in onPause and onStop methods and then "reactivated" in onResume method

 

Android Activity Lifecycle

 

Activity Creation and Starting

IMPORTANT: this will be more fully described with more detailed examples when we get to Intents.


(Up to now you have seen many examples where the Application has ONE Activity and it is started (launch) automatically when the Application is run....this is because it is declared in the AndroidManifest.xml file with the category <category android:name="android.intent.category.LAUNCHER"/> ) --- THIS IS WHY WE NEVER HAD TO Start our Activity
  

 

What we do when we want to start a different Activity (say ListActivity) from

  • STEP1: Create Activity Class that extends android.app.Activity or one of its many subclasses

    • Part of this will be to as required understand the inherited methods (see lifecylce above) and override them as necessary
    • Example:
    public class LabA3_4521Activity extends Activity {
     /** Called when the activity is first created. */
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           //YOUR CODE HERE
     }
    
    
      //OTHER METHODS OVERRIDDEN OR YOUR OWN
    
    }
     
    
    
    //another Activity class
    public class MyListActivity extends ListActivity {
    
      **********CODE HERE *******
    
    
    }
                
  • STEP2: start your activity using Intents (see complete details on this)

    • (Up to now you have seen many examples where the Application has ONE Activity and it is started (launch) automatically when the Application is run....this is because it is declared in the AndroidManifest.xml file with the category <category android:name="android.intent.category.LAUNCHER"/> ) --- THIS IS WHY WE NEVER HAD TO Start our Activity
    • If you have more than One Activity you will be starting one in Code. ----> USE startActivity() method which is an inherited method of the Activity class -- -SO you will be starting another activity from within a different activity

     

    Example:


  • NOTE: you pass the name of the Activity class to the Intent --the Intent is used to create the Activity Class Instance.

    public class LabA3_4521Activity extends Activity {
     /** Called when the activity is first created. */
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
           //YOUR CODE HERE
     }
    
    
      //start the other Activity --- possibly triggered by some GUI event in this LabA3_45221Activity
     public void startListActivity() {
    
           startActivity(new Intent("packagename.MyListActivity");
     }
    
    }
  • STEP3: When you are done with the Activity you can ask to kill it

    • use the finish() method or related finish*() methods (see API)

      Example

      public class MyListActivity extends ListActivity {
      
             ************CODE HERE *************
      
      
      
        //method that may be called on this Activity 
        //  maybe as result of hitting a "end" button that is in the GUI of this Activity
        //   so the End button's OnClickListener will have its onClick method call endThisActivity()
        public void endThisActivity(){
            finish();
      
        }
      
      }
      
      
      
      
      
      
      
      
                  

      See API for details on all the possible finish methods

    public void finish ()

    • Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

    public void finishActivity (int requestCode)

    • Parameters
    requestCode The request code of the activity that you had given to startActivityForResult(). If there are multiple activities started with this request code, they will all be finished.

    public void finishActivityFromChild (Activity child, int requestCode)

    • This is called when a child activity of this one calls its finishActivity().
    • Parameters
    child The activity making the call.
    requestCode Request code that had been used to start the activity.

    public void finishFromChild (Activity child)

    • This is called when a child activity of this one calls its finish() method. The default implementation simply calls finish() on this activity (the parent), finishing the entire group.
    • Parameters
    child The activity making the call.
 

© Lynne Grewe