activities and ntentssite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · activity callbacks...

55
1

Upload: others

Post on 24-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

1

Page 2: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

ACTIVITIES AND INTENTS,

Page 3: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started
Page 4: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started
Page 5: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started
Page 6: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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();

}

Page 7: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 8: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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().

Page 9: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 10: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 11: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 12: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started
Page 13: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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:

Page 14: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started
Page 15: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

INTENTS

Reading:

Book: Beginning Android 4 Application Development

pages 53- 68

Page 16: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 17: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 18: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE

Mainfest.XML file

Page 19: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

main(UI) secondactivity(UI)

Each activity is made up of a ui component (eg. Main.Xml)

and a class component (eg. Usingintentactivity.Java)

Page 20: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

main.xml File

Page 21: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

secondactivity.xml File

Page 22: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 23: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

SecondActivity.java file

Page 24: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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:

Page 25: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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:

Page 26: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

RETURNING RESULTS FROM AN INTENT

Page 27: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE

secondactivity.xml file

Page 28: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE..)

UsingIntentActivity.java

Page 29: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE..)

UsingIntentActivity.java

Page 30: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

Example (continue..)

SecondActivity.java

Page 31: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

Example (continue..)

SecondActivity.java

Page 32: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE..)

Page 33: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 34: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE

secondactivity.xml file

Page 35: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

PassingDataActivity.java file

Page 36: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

PassingDataActivity.java file

Page 37: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

Page 38: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

Page 39: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

Page 40: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 41: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 42: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 43: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 44: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

CALLING BUILT-IN APPLICATIONS USING

INTENTS

You use the parse() method of the Uri

class to convert a URL string into a Uri

object.

Page 45: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 46: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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:

Page 47: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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

Page 48: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

INTENT RESOLUTION

Page 49: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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:

Page 50: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE

For details return to the textbook page 93

Consider the mybrowser activity intent-filter:

To start the activity from another activity:

Page 51: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

Page 52: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE (CONTINUE…)

The added benefit of using the createChooser()method is when no activity matches your Intentobject, your application will not crash.

Page 53: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

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.

Page 54: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

EXAMPLE

In this case, the following code will directly invoke the

MyBrowerActivity activity:

Page 55: ACTIVITIES AND NTENTSsite.iugaza.edu.ps/rsalamah/files/2015/02/lecture3.pdf · ACTIVITY CALLBACKS onRestart() Called after your activity has been stopped, prior to it being started

END

Any Questions

?