Android Programming




      Lesson 6
Intent and Activity

    NGUYEN The Linh
Android Programming


Contents


     1     Understanding Intents

     2     Activity


     3     Exercise 6




                          2
Android Programming


Intent and Activity




         Understanding Intents




                  3
Android Programming


 Understanding Intents


                         http://blog.nikmesoft.com



Verbs                                     URL




                   HTTP

                     4
Android Programming


Understanding Intents

 Just as a Web browser knows how to process a verb
  + URL pair, Android knows how to find activities or
  other application logic that will handle a given intent.

 You can use intents to represent actions.

 At the simplest level, an intent is an action that you
  can tell Android to invoke.




                            5
Android Programming


Understanding Intents




                    6
Android Programming


Understanding Intents

 Notes:
    android:exported: Whether or not the activity can be launched
     by components of other applications — "true" if it can be, and
     "false" if not.

    The general convention for an action name is <your-package-
     name>.intent.action.YOUR_ACTION_NAME.




                                 7
Android Programming


Understanding Intents

 Available Intents in Android
    http://developer.android.com/guide/appendix/g-app-intents.html
    A browser application to open a browser window
    An application to call a telephone number
    An application to present a phone dialer so the user can enter
     the numbers and make the call through the UI
    A mapping application to show the map of the world at a given
     latitude/longitude coordinate
    A detailed mapping application that can show Google stree
     views

                                 8
Android Programming


Event Listeners and Callback Methods

 Example 6.1 6.2




                    9
Android Programming


Understanding Intents

 Using Extra Information
    An extra can provide more information to the component that
     receives the intent.

    This extra information is repre-sented by an Android class called
     android.os.Bundle.

    The following two methods on an Intentclass provide access to
     the extra Bundle:


                                 10
Android Programming


Understanding Intents

 Using Extra Information (cont.)




                          11
Android Programming


Understanding Intents

 Using Extra Information (cont.)




                          12
Android Programming


Understanding Intents

 Using Components to Directly Invoke an Activity
    Android also provides a more direct way to start an activity: you
     can specify the activity’s ComponentName, which is an
     abstraction around an object’s package name and class name.




                                  13
Android Programming


Understanding Intents

 Using Components to Directly Invoke an Activity
  (cont.)
    ComponentName wraps a package name and a class name
     together. For example, the follow-ing code invokes the
     SubActivity:




                              14
Android Programming


Understanding Intents

 Using Components to Directly Invoke an Activity
  (cont.)
    You can also use the class name directly without constructing a
     ComponentName.




    You should register the activity in the AndroidManifest.xml file
     like this:
       •   <activity android:name=".SubActivity">


                                        15
Android Programming


Understanding Intents

 Using Components to Directly Invoke an Activity
  (cont.)

    No intent-filters are necessary for invoking an activity directly
     through its class name or component name.




                                   16
Android Programming


Intent and Activity




               Activity




                 17
Android Programming


Activity

 Activity Lifecycle
    Most management of the life cycle is done automatically by the
     system via the activity stack.
    The activity class has the following method callbacks to help you
     manage the app:
        •   onCreate()
        •   onStart()
        •   onResume()
        •   onPause()
        •   onStop()
        •   onRestart()
        •   onDestroy()
                                 18
Android Programming


Activity




                Activity
                Lifecycle




           19
Android Programming


Activity




                  Saving
                  activity
                   state




           20
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)



   Activity A          Activity B         Activity C




                           21
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)




    Activity A                            Activity B




                          22
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)




   Activity A                             Activity B



                         finish();
                           23
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)




                          24
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)
    The Request Code: The request code that was used to launch
     the returning sub-Activity

    A Result Code: The result code set by the sub-Activity to
     indicate its result. It can be any inte-ger value, but typically will
     be either Activity.RESULT_OK or
     Activity.RESULT_CANCELLED.




                                    25
Android Programming


Activity

 Launching Activities and Sub-Activities(cont.)
    If the sub-Activity closes abnormally or doesn’t specify a result
     code before it closes, the result code is
     Activity.RESULT_CANCELED.

     Data: An Intent used to bundle any returned data.




                                   26
Android Programming


Exercise 6




             27
Android Programming

[Android] Intent and Activity