How to navigate between Android Activities 1 Linking Activities using Intents 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 ID: 546041
Download Presentation The PPT/PDF document "Linking Activities using Intents" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Linking Activities using Intents
How to navigate between Android Activities
1
Linking Activities using IntentsSlide2
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 ActivityThe return of controls can carry data back to the calling activityComparable to calling a method in Java.You use the keyword return to get back to the calling method.Example: IntentsNoData
2
Linking Activities using IntentsSlide3
Different ways of creating an Intent object
Creating an Intent object linking to an Activity in
the same projectIntent intent = new Intent(this, Activity2.class)
Creating an Intent object linking to an Activity in
another
projectIntent 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>
3
Linking Activities using IntentsSlide4
The ”starter pattern”
Leave more responsibility
to the called Activity, and less
responsibility
to the
calling Activity.Code is easier to readExample: IntentsNoDataLinking Activities using Intents4Slide5
Passing data using an Intent object:
Sending dataYou can pass data from one Activity to another using the Intent object
Example: Sending dataIntent intent = new Intent(this, AnotherActivity.class
);
intent.putExtra
(“name”, “Anders”); // Generally putExtra(key, value)startActivity(intent);5Linking Activities using IntentsSlide6
Passing data using an Intent object:
Reading dataThe receiving Activity can read the data.
Example: Reading data from an IntentIntent intent = getIntent();
String name =
intent.getStringExtra
(“name”);Example: IntentsWithData6Linking Activities using IntentsSlide7
Returning results from an Intent
When the called Activity returns to the calling Activity, it can send data (results)
startActivity(…) No resultsstartActivityForResult(…) Results expectedstartActivityForResult(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: IntentsWithDataBack
Linking Activities using Intents
7Slide8
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 DialogsShown on top of another ActivityExample: AndroidManifest.xml
<activity
android:name=".NameDialogActivity" android:theme="@android:style/Theme.Dialog"> </activity>Linking Activities using Intents8Slide9
Navigation to a another application using Intents
Android has several built-in applicationsBrowser, phone, etc.
You can navigate from you Activity to the built-in applications using Intent pairsCommon intentshttps://developer.android.com/guide/components/intents-common.html
9
Linking Activities using IntentsSlide10
Implicit intents
Action
Describes what to be doneExamples: View an item, Edit an item, etc.Action examples: ACTION_VIEW, ACTION_DIAL, ACTION_PICKData
Describes which ”object” the intent affects
http://www.google.com
Tel:+4560609528Geo:38.999,-44.44Content://contactsCode exampleIntent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));Example: IntentsImplicit
10
Linking Activities using IntentsSlide11
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, chapter 3
Linking Activities using Intents
11