1 / 10

Linking Activities using Intents

Linking Activities using Intents. How to navigate between Android Activities. 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

kiril
Télécharger la présentation

Linking Activities using Intents

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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: IntentsPersonalInfo, Eclipse project Linking Activities using Intents

  5. 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); Linking Activities using Intents

  6. 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: IntentsPersonalInfo, Eclipse project 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" android:theme="@android:style/Theme.Dialog"> </activity> Linking Activities using Intents

  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 Linking 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: IntentsForStandardApps Linking 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 Intents

More Related