1, when the other activity is called in order to get the value returned, if you choose the Address Book to enter the address book just return a phone number and so on, we can call Startactivityforresult ();
Intent intent=new Intent (); Intent.setaction ("Com.example.multiactivity.SecondActivity3"); Intent.addcategory (" Android.intent.category.DEFAULT "); Bundle B=new Bundle (); B.putstring ("Name1", "ZP"); B.putstring ("Name2", "db"); Intent.putextras (b); Startactivityforresult (Intent, 0); The //0 parameter is the request code, which is typically set to 0, which is used to identify which request returned data when multiple activity is invoked.
2, usually in the click event, such as the ListView item, click Select Contact, return to the contact, that is, in the called Activity to save the data intent, and call Finnish return data
Intent in=new Intent () In.putextra ("Name3", "Cy"); Setresult (0, in) ; //0 Here is the result code, the user can set a special meaning to the result code, for example, if the network operation, can be defined as the result code 200 is the return of the connection is successful, the exception occurs when the return 403 connection error finish ();
3, in the return activity to overwrite onactivityresult (int requestcode, int resultcode, Intent data) method,
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data); SYSTEM.OUT.PRINTLN ("Request code:" +requestcode); SYSTEM.OUT.PRINTLN ("Result code:" +resultcode); The result code is set for the return time if (requestcode==0)//All of the returned data operations are called when the function is completed, when the value returned by the activity is called, Processing {String st= "1" according to the request code set by Startactivityforresult , if (data!=null) St=data.getstringextra ("Name3"); Ed.settext (ST);}
}
For example, click "Select First Contact" When you set Requestcode to 0, click "Select Second Contact" to set the request code to 1, in Onactivityresult (int requestcode, int resultcode, Intent Data) method, according to the returned Requestcode to take the corresponding action, the relative should be in the edittext to set the phone number.
Android Learning-Call Onactivityresult () to receive the returned information