In Android, data transmission between images is mainly implemented through the putextra method of intent. The transmitted data structure is a map data (name/value pair, note that the difference is that the basic data type must be used. The following describes the implementation methods through instances.
This example describes how to transfer the data of image 1 to Image 2, after Image 2 calculates and processes the data transmitted from image 1, it returns the data to image 1.
1. create the base of the entire project according to the following configuration and the method described in step-by-step instance 1 (note the following in androidmanifest. add the secondactivity configuration to XML. For details, refer to instance 1 ):
Project name: exampletwo
Platform: android2.0;
Application name: exampleone
Package name: COM. Example
Activity: firstactivity, secondactivity
Resource file: first. XML, second. xml
First. XML has two buttons:
ID: @ + ID/forwardbyview
Text: forward to second view by view
ID: @ + ID/forwardbystatus
Text: forward to second view by status
Second. XML has a button: backbutton
ID: @ + ID/back
Text: Back to first view
2. add the following code to firstview (for the overload onactivityresult, right-click the class and select Source-> override/implement methods... in the pop-up box, select onactivityresult () and click OK ):
Private void forward_view_event_byview (){
Button button = (button) findviewbyid (R. Id. forwardview );
Button. setonclicklistener (forwardbyviewbutton_listener );
}
Private button. onclicklistener forwardbyviewbutton_listener = new button. onclicklistener (){
Public void onclick (view v ){
Intent intent = new intent ();
Intent. setclass (firstactivity. This, secondactivity. Class );
Intent. putextra ("Param", "Action View ");
Startactivityforresult (intent, 1 );
}
};
Private void forward_view_event_bystatus (){
Button button = (button) findviewbyid (R. Id. forwardstatus );
Button. setonclicklistener (forwardbystatusbutton_listener );
}
Private button. onclicklistener forwardbystatusbutton_listener = new button. onclicklistener (){
Public void onclick (view v ){
Intent intent = new intent ();
Intent. setclass (firstactivity. This, secondactivity. Class );
Intent. putextra ("Param", "action status ");
Startactivityforresult (intent, 2 );
}
};
Private void showreturnvalue (INT requestcode, int resultcode, intent data ){
Bundle extras = data. getextras ();
If (extras! = NULL ){
String returnvalue = extras. getstring ("returnvalue ");
Settitle (returnvalue + "[" + String. valueof (requestcode) + "-" + String. valueof (resultcode) + "]");
}
}
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Super. onactivityresult (requestcode, resultcode, data );
Showreturnvalue (requestcode, resultcode, data );
}
Finally, insert the following code at the end of oncreate () for calling:
Forward_view_event_byview ();
Forward_view_event_bystatus ();
3. Add the following code to secondview:
Private void show_input_values (){
Bundle extras = getintent (). getextras ();
If (extras! = NULL ){
String inputvalue = extras. getstring ("Param ");
Settitle (inputvalue );
}
}
Private void back_view_event (){
Button button = (button) findviewbyid (R. Id. Back );
Button. setonclicklistener (back_listener );
}
Private button. onclicklistener back_listener = new button. onclicklistener (){
Public void onclick (view v ){
Bundle retbundle = new bundle ();
Retbundle. putstring ("returnvalue", "string comes from the second view ");
Intent intent = new intent ();
Intent. putextras (retbundle );
Setresult (result_ OK, intent );
Finish ();
}
};
Insert the following code at the end of the oncreate () function for calling:
Show_input_values ();
Back_view_event ();
Summary: This example shows how to pass the parameter value "Param" in firstview to secondview and shows the transfer methods under two requestcodes, secondview shows how to display the value transmitted by firstview (basic processing), and then return the value named "returnvalue" to firstview, and display it.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258330.aspx