• An optional String containing some result data, possibly a URL to some internal or external resource — for example, an ACTION_PICK Intent typically returns the selected bit of content via this data string.
• An optional Bundle containing additional information beyond the result code and data string.
To demonstrate launching a peer activity, take a peek at the Activities/Launch sample application in the Source Code section at http://apress.com. The XML layout is fairly straightforward: two fields for the latitude and longitude, plus a button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1,2"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dip"
android:paddingRight="4dip"
android:text="Location:"
/>
<EditText android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
android:layout_weight="1"
/>
<EditText android:id="@+id/lon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
<Button android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Me!"
/>
</LinearLayout>
The button’s OnClickListener simply takes the latitude and longitude, pours them into a geo scheme Uri, then starts the activity.
package com.commonsware.android.activities;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LaunchDemo extends Activity {
private EditText lat;
private EditText lon;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.map);
lat = (EditText)findViewById(R.id.lat);
lon = (EditText)findViewById(R.id.lon);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String _lat = lat.getText().toString();
String _lon = lon.getText().toString();
Uri uri=Uri.parse("geo:"+_lat+","+_lon);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}
}
The activity is not much to look at (Figure 24-1).
Figure 24-1. The LaunchDemo sample application, with a location filled in
If you fill in a location (e.g., 38.8891 latitude and -77.0492 longitude) and click the button, the resulting map is more interesting (Figure 24-2). Note that this is the built-in Android map activity — we did not create our own activity to display this map.
Figure 24-2. The map launched by LaunchDemo, showing the Lincoln Memorial in Washington DC
In a Chapter 34, you will see how you can create maps in your own activities, in case you need greater control over how the map is displayed.
Tabbed Browsing, Sort Of
One of the main features of the modern desktop Web browser is tabbed browsing, where a single browser window can show several pages split across a series of tabs. On a mobile device this may not make a lot of sense, given that you lose screen real estate for the tabs themselves.
In this book, however, we do not let little things like sensibility stop us, so let me demonstrate a tabbed browser, using TabActivity and Intents.
As you may recall from Chapter 10, a tab can have either a View or an Activity as its content. If you want to use an Activity as the content of a tab, you provide an Intent that will launch the desired Activity; Android’s tab-management framework will then pour the Activity’s user interface into the tab.
Your natural instinct might be to use an http:Uri the way we used a geo:Uri in the previous example:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://commonsware.com"));
That way, you could use the built-in Browser application and get all of the features that it offers.
Alas, this does not work. You cannot host other applications’ activities in your tabs — only your own activities, for security reasons.
So, we dust off our WebView demos from Chapter 13 and use those instead, repackaged as Activities/IntentTab.
Here is the source to the main activity, the one hosting the TabView: