4.3.6. Intents for Launching Other Activities

Android uses a technique known as intent messaging to communicate information between activities within one app or activities in separate apps. Each Activity can specify intent filters indicating actions the Activity is capable of handling. Intent filters are defined in the AndroidManifest.xml file. In fact, in each app so far, the IDE created an intent filter for the app’s only Activity indicating that it could respond to the predefined action named android.intent.action.MAIN, which specifies that the Activity can be used to launch the app to begin its execution.

An Intent is used to launch an Activity—it indicates an action to be performed and the data on which to perform that action. In this app, when the user touches a search tag, we create a URL that contains the Twitter search query. We load the URL into a web browser by creating a new Intent for viewing a URL, then passing that Intent to the startActivity method, which our app inherits indirectly from class Activity. To view a URL, startActivity launches the device’s web browser to display the content—in this app, the results of a Twitter search.

We also use an Intent and the startActivity method to display an intent chooser—a GUI that shows a list of apps that can handle the specified Intent. We use this when sharing a saved search to allow the user to choose how to share a search.

Implicit and Explicit Intents

The Intents used in this app are examples of implicit Intentswe will not specify a component to display the web page but instead will allow Android to launch the most appropriate Activity based on the type of data. If multiple activities can handle the action and data passed to startActivity, the system will display a dialog in which the user can select which activity to use. If the system cannot find an activity to handle the action, then method startActivity throws an ActivityNotFoundException. In general, it’s a good practice to handle this exception. We chose not to in this app, because Android devices on which this app is likely to be installed will have a browser capable of displaying a web page. In future apps, we’ll also use explicit Intents, which indicate the precise Activity to start. For a more information on Intents, visit

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.204