Activities Presentation

Activities Presentation


  • What is an Activity?
  • How the user interface of an Activity is specified
  • Start other Activities

What is an Activity?

  • Main building block of an app
    • Represents a screen that is presented to the user
    • Has a life cycle with events when it gets created, hidden, revealed, closed etc
  • One app can have any number of Activities
    • In fact, you never start an app - you always start an Activity that happens to live an app
  • Compare it to an HTML page
    • The web site is the app
    • The web page is the Activity
    • Linking to a web page is launching an Activity

Where is the Activity?

  • Specified as an XML element in AndroidManifest.xml
  • Coded as an Activity-derived Java class in
  • onCreate loads the layout XML and displays it
    @Override protected void onCreate(Bundle savedInstanceState) {
  • Android creates icon for it when installed from intent filter in AndroidManifest.xml
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
  • Android starts the specified Activity in AndroidManifest.xml when launching the app
    <activity android:name="se.mobileapplab.example.MainActivity"

User interface of an Activity

  • Defined in layout XML file loaded by your code in onCreate
    • activity_main.xml
  • Root element is a layout
    • <LinearLayout>
  • Layout element contains views that make up the interface
    • <TextView>
    • <Button>
  • Fragments - a collection of views
    • Useful when building more complex user interfaces than we'll need in the course
    • Introduced in Android 4.0

Activity recreation

  • Beware that an Activity object can be destroyed at any time by the system when it is not visible
  • An Activity object is also destroyed and recreated when device changes orientation
    • Can be turned off by setting in AndroidManifest.xml
  • Most built-in views will remember and restore their values
  • All other values must be stored persistantly by your code
    • Store as SharedPreferences
    • Store in file
    • Store in database

Activity life cycle

  • Methods in your class called during life cycle
    • onCreate - when created after being gone from memory
    • onStart - when app regains control
    • onResume - when Activity is at front of screen
    • onPause - when Activity is in background of screen
    • onStop - when app looses control to other app
    • onDestroy - when Activity is removed from memory

Start other application

  • Android has something similar to file extension associations
    • Applications can register to handle different actions
    • Such a specification is called an Intent
    • By using Intents, no need to know name of other app
  • Create an Intent object with an Action
    Intent intent = new Intent(Intent.ACTION_DIAL);
  • Launch other application that can handle Intent

Start other application with data

  • Send extra data to the other application
    • Might do something by default with that data
  • Extra data as a URI - Universal Resource Identifier
    • Specify a Uri object as second argument to new Intent
    • Create Uri object from String
      Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0123456789"));
  • Extra data as values
    intent.putExtra("sms_body", "Hi there. How are you?");

Phone-related intents

  • Start the dialer with a prepared phone number
    Intent dial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0123456789"));
  • Start the messaging app with a specific phone number and contents
    Intent sms = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:0123456789"));
    sms.putExtra("sms_body", "Hi there. How are you?");
  • Call a phone number immediately. Requires the CALL_PHONE permission!
    Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0123456789"));

Web-related intents

  • Start the web browser and go to web site
    Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
  • Use the web browser to search for something
    Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
    search.putExtra(SearchManager.Query, "android settings intent example");
  • Share a text to any registered application (like Facebook)
    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_SUBJECT, "Good morning!");
    share.putExtra(Intent.EXTRA_TEXT, "Time to get up. This will be a good day.");

Going back

  • Back button
    • Concept of "moving forward" when launching new activities and then "going back" with a Back button, is all handled by Android
  • Back event
    • Can be handled in your Activity class
    • Can be blocked
    • Home button can not be blocked

Add more activities to your own app

  • For better organization of your app
  • Built-in back button handling will automatically switch back to previous activity
  • Steps for creating a new Activity
    • Create a Java class derived from Activity with the onCreate method
    • Create a new layout XML resource file for defining the user interface
    • Add XML element for Activity to AndroidManifest.xml
  • Development environment will have command to automate all steps

Start an Activity in the same app

  • Use an Intent object and the Activity derived class
    Intent other = new Intent(this, OtherActivity.class);
  • Quit the other Activity and go back
    • This is the same as pressing the back button of device

Send data to your other Activity

  • Specify before starting it
    String someValue = "London";
    Intent other = new Intent(this, OtherActivity.class);
    other.putExtra("city", someValue);
  • Retrieve the data in the other Activity
    String sameValueHere = getIntent().getStringExtra("city");
  • Retrieval via getIntent can be done anywhere in the other app
    • Usually you read the data in onCreate and store it in class members if they are needed in other locations of the other Activity.

Get a result back from an Activity

  • Start the Activity with startActivityForResult
    • Include integer that is your own identifier to separate different results from each other
  • Example
    Intent intent = new Intent(this, MyOtherActivity.class);
    startActivityForResult(intent, 333);
  • Must create specific method in Activity to be called with result
  • This method will be called automatcially by the system when the started Activity is finished
    protected void onActivityResult(int request, int result, Intent data) {

Return result from started Activity

  • Other Activity will call setResult before finishing
    • All returned data is stored as "extras" in an Intent
    • This Intent object does not start anything, it is just used to hold data
    • Also includes an integer identifier for onActivityResult that you can use however you like
  • Return the result
    Intent data = new Intent();
    data.putExtra("name", "Adam");
    setResult(1000, data);
  • Check for the result in the originating Activity
    protected void onActivityResult(int request, int result, Intent data) {
      if (request==333 && result==1000) {
        String name = data.getStringExtra("name");