CS260 Intro to Java & Android...

Preview:

Citation preview

CS260 Intro to Java & Android07.AndroidIntents

Winter 2020

Winter 2020 CS260 - Intro to Java & Android 1

Application Organization

• The Android Architecture is designed so an application is composed of well-defined Activities

• One Activity is the main Activity launched by the launcher

• Each Activity is reachable via intentsWinter 2020 CS260 - Intro to Java & Android 2

Intent

• An intent is a messaging object used to request some action from another app component

– Activity

– Service

– Broadcast receiver

– Content provider

Winter 2020 CS260 - Intro to Java & Android 3

Starting an Activity

• An Activity represents a single screen in an app

• A new Activity instance can be started by passing an Intent to a start activity method

• Intent

– describes the activity to start

– contains any necessary dataWinter 2020 CS260 - Intro to Java & Android 4

Starting an Activity

• startActivity () starts another activity and returns no result

• startActivityForResult () returns a result as a separate Intent object in the calling activities onActivityResult () callback

Winter 2020 CS260 - Intro to Java & Android 5

Intent

• An intent is also a message facility for late run-time binding between components in the same or different applications

• What is binding time?

• What are possible binding times?

Winter 2020 CS260 - Intro to Java & Android 6

Starting new Activities

• Activities can be started:

1. explicitly – a class to load is specified

2. implicitly – an action to be performed on a piece of data is requested

Winter 2020 CS260 - Intro to Java & Android 7

Explicitly Starting An Activity

• One Activity shows up in the launcher

• Other Activities need to be reached somehow

• Intents are messages

• Android is about intents and receivers of intents

• Explicitly starting an Activity:startActivity (intent);

Winter 2020 CS260 - Intro to Java & Android 8

Two Explicit Scenerios

Consider Activity (A1) launches Activity (A2)

• Question: Does A1 need a result from A2?

• If so, then launch A2 as a sub-activity so A1 knows when A2 is done

• If not, then launch A2 as a regular ActivityWinter 2020 CS260 - Intro to Java & Android 9

Explicit Activity Startup

• Activity startup requires:

– an intent

– a choice of how to start the Activity

• Remember, intents “encapsulate a request” for some other component (Activity right now) to do something

Winter 2020 CS260 - Intro to Java & Android 10

startActivity• The easiest way to start an Activity is:

startActivity (new Intent (this, Classname.class);

The arguments for Intent in the above case are:

this – a Context of the application package implementing the class

Classname.class – the component class that is to be used for the intent

Winter 2020 CS260 - Intro to Java & Android 11

Remember

• The previous statement will launch the Activity Classname

• You MUST make sure the Activity classnameexists in the AndroidManifest.xml file. This will be automagic if you add the Activity correctly.

Winter 2020 CS260 - Intro to Java & Android 12

Problem #11. When the Button HELP is pressed in the main

Activity of your Addition application, you are to start an Activity that displays the following:

Pressing COMPUTE - performs the addition

Pressing CLEAR - clears all input fields

Winter 2020 CS260 - Intro to Java & Android 13

Problem #1How?

1. Add a second Blank Activity to the package called Help. A layout called activity_help.xml will be created.

2. Modify MainActivity.java to start the new Activity Help when the HELP button is pressed

Winter 2020 CS260 - Intro to Java & Android 14

JAR Files• Java applications can be packaged in a Java archive

(JAR) file

• JAR files

– package file format containing classes, resources, ... in one file for distribution

– can be shared with other developers

– in IntelliJ are called an "artifact"

– can be run in a command prompt byjava -jar file.jar

Winter 2020 CS260 - Intro to Java & Android 15

Problem #21. Copy the folder Addition from CS260-01Public\2020

on turing

2. Run the program to make sure it works

3. Follow the link "Creating JAR files" on the course Web site

4. From the command prompt, find the JAR file in the artifacts folder and run

Winter 2020 CS260 - Intro to Java & Android 16

Problem #3Time to hook up your Java Addition with your Android MyAddition

1. Using Windows File Explorer, find the libs folderMyAddition->app->libs

2. Place a copy of the jar file in the libs folder

3. File->Project Structure->Dependencies

4. Select app

5. Under Declared Dependencies hit the + then Jar Dependency

6. You should be able to finish from there

Winter 2020 CS260 - Intro to Java & Android 17

Problem #3 Continued7. In your Android application MyAddition, create a

new Addition object using the Jar file

8. Call the getter from the Addition object to add two numbers

9. Pass to setText

10. Test the app

This is how you will hook up Minesweeper Java with Minesweeper Android. Pretty cool huh???

Winter 2020 CS260 - Intro to Java & Android 18

Recommended