Activities Presentation


  • Understand what an Activity is
  • Create a user interface inside an Activity
  • 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
  • Compare it to an HTML page
    • Without an HTML page you don't have a web site. One page can link to another with the same site or a page on a unrelated web site. The same is true for Activities.

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(s)
    • activity_main.xml
    • fragment_main.xml
  • What happens
    • onCreate is called by system
    • setContentView loades the activity_main.xml into the Activity
    • A fragment is created with the contents from fragment_main.xml and added to the Activity

User interface without Fragment

  • Fragments - a collection of views
    • Useful when building more complex user interfaces than we'll need in the course
    • Not a requirement if you don't need them
    • All user interface can be placed in activity_main.xml instead
  • Remove Fragments from default project
    • Delete the res/layout/fragment_main.xml file (yes, "Delete Anyway" in Android Studio)
    • Delete the code snippet from onCreate in
      if (savedInstanceState == null) {
        .add(, new PlaceholderFragment())
    • And now you don't need the PlacementFragment class either (not an error to keep it)

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

Start other application

  • Android has something similar to file extension associations
    • Applications can register to handle different types of "actions". Such a specification is called an "Intent" on Android. By using Intents, you don't need to know about which other applications are installed on the device but still take advantage of their features from your application.
  • Create an Intent object with an Action (and optional specification)
    Intent intent = new Intent(Intent.ACTION_DIAL);
  • Launch other application with startActivity

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 to a prepared phone number with a text
    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.");

Add more activities to your own app

  • For better organization 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");

More information

  • Book references
    • The Busy Coder's Guide 3.7
      • Rewriting your first project, page 63
      • Launching activities, page 297