working with multiple activities. slide 2 introduction working with multiple activities creating...

31
Working with Multiple Activities

Upload: phillip-golden

Post on 18-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Working with Multiple Activities

Page 2: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 2

Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to activities Putting together the

AndroidManifest.xml file

Page 3: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 3

Multiple Activities (Introduction) When the user switches between

activities, one activity is stopped, and the other is started Remember the activity lifecycle

Note that you can start activities that belong to other applications

A mail program for example We will not do that yet We will work within our own application

Page 4: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 4

Starting an Activity To start a new activity, you call startActivity() passing an Intent as the argument The Intent describes the activity that you

want to start The user can then select from the possible

intents (external) Or the activity just runs (external)

An activity can also return results

Page 5: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 5

Creating a Second Activity Remember that an activity is just a class So we just

add a new class Inherit from android.app.Activity Add the activity to the manifest

Page 6: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 6

Creating aSecond Activity

Page 7: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 7

Add to Manifest

Page 8: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 8

Resulting Manifest

Page 9: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 9

Creating a Layout You have created portrait and landscape

layouts When working with multiple activities,

you need to create layouts for those activities In the project Explorer, click New, Other

Page 10: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 10

Creating a Layout (Illustration) In the first dialog, select Android XML

Layout File In the second dialog, select the desired

layout

Page 11: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 11

Creating a Layout (Illustration) Set configuration options Note the file is added to the res/layout

folder by default

Page 12: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 12

Introduction to Intents Before we can explicitly start an

activity, you need to understand intents An intent is an object used to

communicate with the OS Intents are used with activities, services,

broadcast receivers, and content providers We have only discussed activities so far

You can use intents to tell the Android OS which activity to start

Page 13: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 13

Intents (Philosophically) As the name implies, it’s an intention to

do an action It’s a message to say

I did something I want something to happen

When you create an intent, you are saying that you want to move from one activity to another

Page 14: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 14

Intents (Types) There are two types of intents Explicit intents are used with a

Context and Class object to start an activity within your application

Implicit intents are used to start activities outside of your application Not yet

THERE ARE MUCH MORE TO INTENTS THAN DISCUSSED HERE

Page 15: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 15

Intent (Constructor) This is but one of the many constructors

The first argument contains the current activity

The second argument contains the class name of the activity to be started

Page 16: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 16

Intent (Creating) The following appears in the current

activity (MainActivity.this) and starts the second activity (Page2Activity.class)

Page 17: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 17

Starting an Activity (Illustration)

startActivity is called with an Intent. The ActivityManager uses that intent to determine the activity to start

Page 18: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 18

Passing Data to Activities You can pass data from one activity to

anther through intent extras Extras are just arbitrary data that can be

passed to an intent An extra is a key / value pair that is passed

to an intent The started activity can read this extra

data

Page 19: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 19

Creating (Writing) an Extra First, create the Intent as shown before Second, call putExtra()

First argument contains the key Second argument contains the value

Page 20: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 20

Creating (Writing) an Extra The first argument contains the key,

which here is a constant “LastActivity” The second contains the value

“MainActivity”

Page 21: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 21

Reading an Extra The getIntent() method returns an Intent object

It’s methods get values of a particular type getBooleanExtra(), getByteExtra(), getIntExtra(), etc…

Page 22: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 22

Reading an Extra (Example) Read an extra into a TextView

Page 23: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 23

Returning an Activity Result (Introduction) There are times when we need to return

a result from an activity We start an activity as before, however,

pass a request code when starting the activity The request code is an integer It is send to the child (started) activity The result is then returned to the parent

Page 24: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 24

Returning an Activity Result Call startActivityForResult()

instead of startActivity() The first argument contains the intent The second argument contains the integer

result that will be returned The result is returned through

overriding onActivityResult() in the parent activity

Page 25: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 25

Returning an Activity Result To return the result, you call one of two

forms of setResult() The first returns a result code Typically

Activity.RESULT_CANCELED Activity.RESULT_OK

The second return the result code and intent data

Call finish() to return from the activity

Page 26: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 26

Activity Result Example (1) Start the activity as before with startActivity except call startActivityForResult The first argument contains the activity as

before The second argument contains an integer

request code It answers the question, from which activity

are we returning

Page 27: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 27

Activity Result Example (2) Here we return a code but no data

The intent is empty and has no extras RESULT_CANCELED is an android constant We call finish to return to the calling

Activity

Page 28: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 28

Activity Result Example (2a) Here we return a code and data

The Intent has extra data We change the return code

Page 29: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 29

Activity Result Example (3) In the calling activity, handle the onActivityResult() event

Page 30: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 30

AndroidManifest.xml (Multiple Activities) The AndroidManifest.xml must list all of

the activities Remember one activity is designated as

the launcher activity

Page 31: Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to

Slide 31

AndroidManifest.xml(Multiple Activities)