Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linking Activities using Intents

Similar presentations


Presentation on theme: "Linking Activities using Intents"— Presentation transcript:

1 Linking Activities using Intents
How to navigate between Android Activities Linking Activities using Intents

2 Navigation from an Activity to another Activity
Create an Intent object Intents can be created in different ways Intents can carry data from the calling Activity to the called Activity Start the other Activity, using the Intent object When the called Activity ends (finish() method) the control is returned to the calling Activity The return of controls can carry data back to the calling activity Comparable to calling a method in Java. You use the keyword return to get back to the calling method. Example: IntentsSimple Linking Activities using Intents

3 Different ways of creating an Intent object
Creating an Intent object linking to an Activity in the same project Intent intent = new Intent(this, Activity2.class) Creating an Intent object linking to an Activity in another project Intent intent = new Intent(”dk.easj.Activity2”); The other project must register the Intent name in the AndroidManifest.xml <activity android:name=".Activity2" <!-- .class name --> android:label="Activity 2"> <intent-filter > <action android:name="dk.easj.Activity2" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> Linking Activities using Intents

4 Passing data using an Intent object: Sending data
You can pass data from one Activity to another using the Intent object Example: Sending data Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra(“name”, “Anders”); // Generally putExtra(key, value) startActivityForResult(intent, request_Code); Linking Activities using Intents

5 Passing data using an Intent object: Reading data
The receiving Activity can read the data. Example: Reading data from an Intent Intent intent = getIntent(); CharSequence name = intent.getCharSequenceExtra(“name”); // CharSequence is s super-class of String Example: IntentsWithData Linking Activities using Intents

6 Returning results from an Intent
When the called Activity returns to the calling Activity, it can send data (results) startActivity(…) No results startActivityForResult(…) Results expected startActivityForResult(intent, request_Code); Callback method in calling Activity: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == request_Code) { // which request? Same request_Code as in the startActivityForResult(…) if (resultCode == RESULT_OK) { // or RESULT_CANCELLED final String fullData = data.getData().toString(); } Example: IndentDataExample Linking Activities using Intents

7 Making activities look like dialogs
With an Intent you go to another Activity. Dialogs on the other hand usually show up on top of another Activity. Activities can look like Dialogs Shown on top of another Activity Example: AndroidManifest.xml <activity android:name=".NameDialogActivity" </activity> Linking Activities using Intents

8 Navigation to a another application using Intents
Android has several built-in applications Browser, phone, etc. You can navigate from you Activity to the built-in applications using Intent pairs Common intents Linking Activities using Intents

9 Intent pairs (action, data)
Describes what to be done Examples: View an item, Edit an item, etc. Action examples: ACTION_VIEW, ACTION_DIAL, ACTION_PICK Data Describes which ”object” the intent affects Tel: Geo:38.999,-44.44 Content://contacts Code example Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(" Example: IntentsForBuiltinApps Linking Activities using Intents

10 Linking Activities using Intents
Using Intent Filters If you want other activities to invoke you activity, you must specify some Intent Filters in the AndroidManifest.xml file. Example: Messenger Head First, page 103 Linking Activities using Intents


Download ppt "Linking Activities using Intents"

Similar presentations


Ads by Google