Linking and passing parameters between activities are mainly implemented through an object of intent android.
First, create a mainactivity:
Package COM. example. androidtest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. window; import android. widget. toast;/*** Android portal file * @ author Zhuli. zhul * @ date 2013 3:09:14 */public class mainactivity extends activity {private string tag = "mainactivity"; private int request_code = 1; /*** Page Layout */protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. activity_main); // sets a view template log. D (TAG, "in the oncreate () event");}/*** click to open a new activity page * @ Param view */Public void onclickstartactivity (view) {log. D (TAG, "onclickstartactivity"); // The following parameters can be passed and clicked to open a new activity intent I = new intent ("android. intent. action. openactivity "); I. putextra ("str", "this is to pass a parameter"); I. putextra ("int", 100); startactivityforresult (I, request_code);}/*** parameters returned after closing the activity page opened by monitoring */Public void onactivityresult (INT requestcode, int resultcode, intent data) {toast. maketext (getbasecontext (), Data. getdata (). tostring (), toast. length_short ). show ();}}
Create a layout file activity_main.xml in the layout folder.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/btn_dialogs" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/startactivity" Android: onclick = "onclickstartactivity"/> </linearlayout>
After you click the button on the layout page, the onclickstartactivity method is triggered. This method starts an openactivity:
At the same time, the STR and INT parameters are passed.
/*** Click to open a new activity page * @ Param view */Public void onclickstartactivity (view) {log. D (TAG, "onclickstartactivity"); // The following parameters can be passed and clicked to open a new activity intent I = new intent ("android. intent. action. openactivity "); I. putextra ("str", "this is to pass a parameter"); I. putextra ("int", 100); startactivityforresult (I, request_code );}
Create an openactivity:
In openactivity, a layout page is created, and the layout page contains parameters for outputting mainactivity through messages.
In openactivity, there is an onclick method, which is mainly used to trigger after clicking a button on the layout page. The main onclick method is to return mainactivity and pass the parameter "Hello World" to mainactivity.
Package COM. example. androidtest; import android. app. activity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. window; import android. widget. toast; public class openactivity extends activity {/*** Page Layout */protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); setcontentview (R. layout. activity_open); // set a view template log. D ("second", "in the oncreate () event"); // capture the string STR = getintent () parameter passed after mainactivity opens the activity (). getstringextra ("str"); toast. maketext (getbasecontext (), STR, toast. length_short ). show (); int I = getintent (). getintextra ("int", 0); toast. maketext (getbasecontext (), "Age:" + I, toast. length_short ). show ();}/*** click the button event and return to the mainactivity page * @ Param view */Public void onclick (view) {intent DATA = new intent (); data. setdata (URI. parse ("Hello World"); setresult (1, data); finish ();}}
Create a layout file activity_open:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <analogclock Android: id = "@ + ID/analogclock1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/btn_dialogs" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/progressdialog" Android: onclick = "onclick"/> </linearlayout>
Modify androidmanifest. xml:
To add or modify an activity, you must modify it in androidmanifest. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. example. androidtest "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "Android: targetsdkversion = "17"/> <application Android: allowbackup = "true" Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name" Android: theme = "@ style/apptheme"> <! -- Default activity --> <activity Android: Name = "com. example. androidtest. mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <! -- Newly added activity --> <activity Android: Name = ". openactivity "Android: Label =" Open activity "> <intent-filter> <action Android: Name =" android. intent. action. openactivity "/> <category Android: Name =" android. intent. category. default "/> </intent-filter> </activity> </Application> </manifest>
Note:
Open a new activity and pass parameters:
Intent I = new intent ("android. intent. action. openactivity "); I. putextra ("str", "this is to pass a parameter"); I. putextra ("int", 100); startactivityforresult (I, request_code );
Received parameters:
// Capture the string STR = getintent () parameter passed after mainactivity opens the activity (). getstringextra ("str"); toast. maketext (getbasecontext (), STR, toast. length_short ). show (); int I = getintent (). getintextra ("int", 0); toast. maketext (getbasecontext (), "Age:" + I, toast. length_short ). show ();
After the activity is closed and the parameter is passed to the previous activity:
Intent DATA = new intent (); data. setdata (URI. parse ("Hello World"); // pass the setresult (result_ OK, data) parameter; // pass the result_ OK parameter-state data-specific parameter finish ();
Listener callback parameters:
/*** Parameters returned after the activity page opened by monitoring is closed */Public void onactivityresult (INT requestcode, int resultcode, intent data) {// requestcode is the request_code passed by startactivityforresult, the result_ OK parameter in setresult (result_ OK, data) is used to distinguish different activities opened on a page, the main difference is that the callback data status/data is the passed data if (requestcode = request_code) {If (resultcode = result_ OK) {toast. maketext (getbasecontext (), Data. getdata (). tostring (), toast. length_short ). show ();}}}