The Android UI framework requires users to divide their app into activity, which is scheduled via Itent, where a main activity is called by Android's launcher on the desktop. For example, a calendar app that needs to look at the activity of a calendar, view the activity of a single event, edit an event's activity, and so on. In the activity of viewing a calendar, if a user chooses an event, it needs to be handled by viewing the activity of the event. This is the latest app UI framework, and this time we'll learn how to do it through intent.
The relationship between activity
In some business logic, activity1 needs to know whether the activity2 is over, Activity2 is Activity1 subactivity.
In other business logic, Activity1 does not need to understand the operation of the activity2 it has been tuned to, such as opening an attachment in a message, activity1 does not need to know whether to view the attachment Activity2 the end. In this case, the two activity is not a master-slave relationship, but a peer relationship, Activity2 is a regular activity.
Step 1:make The Intent
Intent will encapsulate a request to Android so that other activity or intent receiver knows how to handle it. If the activity that intent wants to launch is written by ourselves, it can easily be achieved by a precisely specified intent, for example:
New Intent (this, helpactivity.class);
Where Helpactivity.class is the component we need to launch. This activity only needs to be defined in the Androidmanifest.xml file, but does not require any inter filter, because it is done by direct request. If we need to hit some data, that is, the URI, request a generic activity, as follows:
Uri Uri=uri.parse ("Geo:" +lat.tostring () + "," +lon.tostring () ");
Intent i=new Intent (Intent.action_view, URI);
Step 2:make The call
Depending on the relationship between the activity, the intent Method startactivity () or Startactivityforresult () to launch another activity. The latter is required to know whether the Activity2 is running at the end, and the results are running.
Startactivityforresult () will pass a number to intent, which is unique for calling activity. Used to differentiate which activity. When the activated activity runs out, it will trigger Onactivityresult () in the calling activity, and we can use this unique number to distinguish which of the transferred activity ends. We will also get the following information:
- Result code, which is set by the Setresult (), is usually Result_ok and result_cancelled, and of course we can define our own return values (from Result_first_ User start definition)
- An optional string that is used to pass some return information, such as a URL to describe a resource, with data such as returning user-picked content in Action_pick.
- Optional bundle to return the required information
Example one: a simple call
Example by inputting latitude and longitude, and then using intent to launch map application. The layout file is as follows, and in passing review the layout of tablelayout:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout. >
<tablelayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
"Android:stretchcolumns=" >
<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/c18_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/c18_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/c18_map" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
android:text= "Show me!"/>
</LinearLayout>
The source code is as follows:
public class Chapter18test1 extends activity{
Private EditText latitude = null;
Private EditText longtitude =null;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.CHAPTER_18_TEST1);
Latitude = (EditText) Findviewbyid (R.id.c18_lat);
Longtitude = (EditText) Findviewbyid (R.id.c18_lon);
Button button = (button) Findviewbyid (R.ID.C18_MAP);
Button.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
String _lat = Latitude.gettext (). toString ();
String _lon = Longtitude.gettext (). toString ();
Because the AVD lacks a map application, the GEO is not triggered in the simulator: As a URI application, an error occurs, and if you still use the emulator, you can browse through the Web. The example uses an emulator.
Uri Uri=uri.parse ("Geo:" + _lat + _lon);
Uri uri=uri.parse ("http://maps.google.com/?q=" +_lat + "," + _lon);
startactivity (New Intent (Intent.action_view,uri));
}
});
}
}
Example two: Tab Browser
We can review the Android learning Note (22): The last example of a multi-page display-tag, which displays acitvity in tab. If we need to show activity as content in tab, we provide a intent to launch the required activity. In this example, we will use the tab content to intent the page.
If we directly use
Intent intent=new Intent (Intent.action_view);
I.setdata (Uri.parse (http://commonsware.com));
And then in tab setcontent (intent), we'll find that we can't fight for work, because in Android, for security reasons, you can't load other apps ' activity in the tab. So we need to write for ourselves. Let's start by writing two browsers that embed WebKit browser.
public class Chapter18test2_1 extends activity{ private WebView browser = null; protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); browser = new WebView (this); Setcontentview (browser); browser.loadurl ("http:// Commonsware.com "); } } |
public class Chapter18test2_2 extends activity{ Private WebView browser = null; protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Browser = new WebView (this); Setcontentview (browser); Browser.loadurl ("http://www.android.com"); } } |
Calling activity's source code is as follows, very simple
public class Chapter18test2 extends
tabactivity{//using tabactivity, there is already a tab layout, so we no longer need the layout XML file.
Private Tabhost host = null;
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
host= Gettabhost ();
host.addtab (host. Newtabspec ("one"). Setindicator ("CW").
SetContent (New Intent ( This , chapter18test2_1.class ) ));
host.addtab ("Host.newtabspec"). Setindicator ("Android").
SetContent ( new Intent ( this , chapter18test2_2.class ));
}
}
We look back at this example code, the same open a WebKit browser URL using two activity, two class, which is quite wasteful. We can pass the URL as intent's extra information. We will modify the above example to pass the extra information through bundles, and in the calling activity, it is easy to add tab.
The activity that is called:
Public
class Chapter18test2_0 extends activity{
Private WebView browser = null;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Browser = new WebView (this);
Setcontentview (browser);
Bundle bundle =
getintent (). Getextras ();
Browser.loadurl (
bundle.getstring("
http_uri"));
}
}
Calling's activity:
public class Chapter18test2 extends tabactivity{
Private Tabhost host = null;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Host=gettabhost ();
AddTab ("One", "CW", "http://commonsware.com");
AddTab ("Both", "Android", "http://www.android.com");
AddTab ("Three", "Blog", "http://blog.csdn.net/flowingflying/");
}
Add a method for adding tab
private void AddTab (string tag, string indicator, string uri) {
Bundle bundle = new bundle ();
Bundle.putstring ("Http_uri", URI);
Intent Intent = new Intent (this,chapter18test2_0.class);
Intent.putextras (bundle);
Host.addtab (Host.newtabspec (tag). Setindicator (indicator). SetContent (intent));
}
}
RELATED Links: My Android development related articles
Android Learning Note (35): Talk about intent (next)-some practices