3Linking Activities using Intents"> 3Linking Activities using Intents">
Download presentation
Presentation is loading. Please wait.
Published byFay Waters Modified over 8 years ago
1
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents
2
Navigation from an Activity to another Activity 1.Create an Intent object Intents can be created in different ways Intents can carry data from the calling Activity to the called Activity 2.Start the other Activity, using the Intent object 3.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 2Linking 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" android:label="Activity 2"> 3Linking 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, Lee page 66 (54) Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra(“name”, “Anders”); // Generally putExtra(key, value) startActivityForResult(intent, request_Code); 4Linking Activities using Intents
5
Passing data using an Intent object: Reading data The receiving Activity can read the data. Example: Reading data, Lee page 67, (54) Intent intent = getIntent(); CharSequence name = intent.getCharSequenceExtra(“name”); // CharSequence is s super-class of String Example: IntentsWithData 5Linking 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: Example, Lee page 52 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: IntentsWithData Linking Activities using Intents6
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" android:theme="@android:style/Theme.Dialog"> Linking Activities using Intents7
8
Navigation to a built-in 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 – https://developer.android.com/guide/component s/intents-common.html 8Linking Activities using Intents
9
Intent pairs (action, data) Action – 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 http://www.google.com Tel:+4560609528 Geo:38.999,-44.44 Content://contacts Code example –Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com")); Example: IntentsForBuiltinApps 9Linking Activities using Intents
10
Using Intent Filters If you want other activities to invoke you activity, you must specify some Intent Filters in the AndroidManifest.xml file. Example: IntentsSimple Linking Activities using Intents10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.