1
ACTIVITIES AND INTENTS,
ACTIVITY CALLBACKS
public class Activity extends ApplicationContext
{
protected void onCreate(Bundle
savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
ACTIVITY CALLBACKS
onCreate()
Called when the activity is first created.This is where you should do all of yournormal static set up: create views, binddata to lists, etc. This method also providesyou with a Bundle containing the activity'spreviously frozen state, if there wasone.Always followed by onStart().
onStart()
Called when the activity is becomingvisible to the user.Followedby onResume() if the activity comes to theforeground, or onStop() if it becomeshidden.
ACTIVITY CALLBACKS
onRestart()
Called after your activity has beenstopped, prior to it being startedagain.Always followed by onStart()
onResume()
Called when the activity will startinteracting with the user. At thispoint your activity is at the top ofthe activity stack, with user inputgoing to it.Always followedby onPause().
ACTIVITY CALLBACKS
onPause() Called when the system is about to start resuming a
previous activity. This is typically used to commit
unsaved changes to persistent data, stop animations
and other things that may be consuming CPU, etc.
Implementations of this method must be very quick
because the next activity will not be resumed until
this method returns. Followed by either onResume() if the activity returns
back to the front, or onStop() if it becomes invisible to
the user.
ACTIVITY CALLBACKS
onStop() Called when the activity is no longer visible to the
user, because another activity has been resumed and
is covering this one. This may happen either because
a new activity is being started, an existing one is
being brought in front of this one, or this one is being
destroyed. Followed by either onRestart() if this activity is
coming back to interact with the user, or
onDestroy() if this activity is going away.
ACTIVITY CALLBACKS
onDestroy() The final call you receive before your activity is
destroyed. This can happen either because the
activity is finishing (someone called finish() on it, or
because the system is temporarily destroying this
instance of the activity to save space). You can
distinguish between these two scenarios with
the isFinishing() method.
Simple exercise to see
the activity lifecycle in
action
each overridden
function is explicit and
a Toast command is
added to show on screen
when the function is
ntered
Run it on an Android
device and try arious
cases:
INTENTS
Reading:
Book: Beginning Android 4 Application Development
pages 53- 68
INTENTS
An intent is an abstract description of an operation to be
performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested
BroadcastReceiver components, and startService(Intent)
or bindService(Intent,ServiceConnection,int) to
communicate with a background Service.
An Intent provides a facility for performing late runtime
binding between the code in different applications.
Its most significant use is in the launching of activities,
where it can be thought of as the glue between activities.
It is basically a passive data structure holding an
abstract description of an action to be performed.
LINKING ACTIVITIES USING INTENTS
An Android application can contain zero or more
activities.
When your application has more than one
activity, you often need to navigate from one to
another.
In Android, you navigate between activities
through an intent.
EXAMPLE
Mainfest.XML file
main(UI) secondactivity(UI)
Each activity is made up of a ui component (eg. Main.Xml)
and a class component (eg. Usingintentactivity.Java)
EXAMPLE (CONTINUE…)
main.xml File
EXAMPLE (CONTINUE…)
secondactivity.xml File
Activities in Android can be
invoked by any application
running on the device
when the activity you want to display is within
the same project as the current activity
EXAMPLE (CONTINUE…)
UsingIntentActivity.java file
EXAMPLE (CONTINUE…)
SecondActivity.java file
RESOLVING INTENT FILTER COLLISION
What happens if another activity (in either the
same or a separate application) has the same
filter name?
the Android OS will display
a selection of activities,
as shown:
RETURNING RESULTS FROM AN INTENT
You may have an activity that prompts the user for
user name and password.
The information entered by the user in that activity
needs to be passed back to the calling activity for
further processing.
If you need to pass data back from an activity, you
should instead use the startActivityForResult()
method.
In order for an activity to return a value to the calling
activity, you use an Intent object to send data back
via the setData() method:
In the calling activity, you need to implement the
onActivityResult() method, which is called whenever
an activity returns:
RETURNING RESULTS FROM AN INTENT
EXAMPLE
secondactivity.xml file
EXAMPLE (CONTINUE..)
UsingIntentActivity.java
EXAMPLE (CONTINUE..)
UsingIntentActivity.java
Example (continue..)
SecondActivity.java
Example (continue..)
SecondActivity.java
EXAMPLE (CONTINUE..)
PASSING DATA TO THE TARGET ACTIVITY
Besides returning data from an activity, it is also
common to pass data to an activity. For example.
In the previous example you may want to set some
default text in the EditText view before the activity is
displayed.
In this case, you can use the Intent object to pass the
data to the target activity.
Please read the example page 63 of the selected
reading.
EXAMPLE
secondactivity.xml file
EXAMPLE (CONTINUE…)
PassingDataActivity.java file
EXAMPLE (CONTINUE…)
PassingDataActivity.java file
EXAMPLE (CONTINUE…)
EXAMPLE (CONTINUE…)
EXAMPLE (CONTINUE…)
BUNDLE/EXTRA
you can use the putExtra() method of an Intent
object to add a name/value pair:
you can also create a Bundle object and then
attach it using the putExtras() method. Think of
a Bundle object as a dictionary object — it
contains a set of name/value pairs.
SETDATA()\ GETDATA()
Another way to pass data to an activity is to use
the setData() method
Usually, you use the setData() method to set the
data on which an Intent object is going to operate
To retrieve the data set using the setData()
method, use the getData() method
CALLING BUILT-IN APPLICATIONS USING
INTENTS
You learned that you can call another activity by
passing its action to the constructor of an Intent
object:
The action in this example is
“net.learn2develop.SecondActivity” is also
known as the component name. This is used to
identify the target activity/application that you
want to invoke.
You can also rewrite the component name as
Explicit Intent
CALLING BUILT-IN APPLICATIONS USING
INTENTS
In Android, intents usually come in pairs: action
and data.
The action describes what is to be performed,
such as editing an item, viewing the content of an
item, and so on.
The data specifies what is affected, such as a
person in the Contacts database.
The data is specified as an Uri object.
the action and data pair describes the operation
to be performed. Examples are:Implicit Intents
CALLING BUILT-IN APPLICATIONS USING
INTENTS
You use the parse() method of the Uri
class to convert a URL string into a Uri
object.
CALLING BUILT-IN APPLICATIONS USING
INTENTS
You can dial a specific number by passing in the
telephone number in the data portion:
If you want to directly call the number:
You need to add permoission to the mainfest
CALLING BUILT-IN APPLICATIONS USING
INTENTS
For some intents, there is no need to specify the
data.
For example, to select a contact from the Contacts
application, you specify the action and then indicate
the MIME type using the setType() method to
indicate the type of data to return:
UNDERSTANDING THE INTENT OBJECT
To summarize, an Intent object can contain the
following information:
Action
Data
Category
ExtraComponent Name
Action Name
Data
Category
Extra Flags
Structure
of an Intent
INTENT RESOLUTION
USING INTENT FILTERS
Earlier, you saw how an activity can invoke another
activity using the Intent object.
In order for other activities to invoke your activity, you
need to specify the action and category within the
<intent-filter> element in the AndroidManifest.xml
file, like this:
EXAMPLE
For details return to the textbook page 93
Consider the mybrowser activity intent-filter:
To start the activity from another activity:
EXAMPLE (CONTINUE…)
EXAMPLE (CONTINUE…)
The added benefit of using the createChooser()method is when no activity matches your Intentobject, your application will not crash.
ADDING CATEGORIES
You can group your activities into categories
by using the <category> element in the
intent filter
when using an Intent object with categories,
all categories added to the Intent object must
fully match those defined in the intent filter
before an activity can be invoked.
EXAMPLE
In this case, the following code will directly invoke the
MyBrowerActivity activity:
END
Any Questions
?