If you want to get the data returned after the new open activity is closed in activity, you need to use the system-provided Startactivityforresult (Intent intent,int requestcode) method to open a new activity, the new activity will be closed to the previous activity back to the data, in order to get the data returned, you must rewrite in the previous activity onactivityresult (int requestcode, int Resultcode,intent data) Method:
Public classMainactivity extends Activity {@Override
ProtectedvoidonCreate (Bundle savedinstancestate) {button button= (Button) This. Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {//Clicking this button will open a new activityPublic void OnClick (View v) {//The second parameter is the request code, can be numbered according to business requirementsStartactivityforresult (NewIntent (mainactivity. This, Newactivity.class),1); }}); } //The first parameter is the request code, which is called Startactivityforresult () passing past values//The second parameter is the result code, which is used to identify which new activity the returned data came from.@Override
protectedvoid Onactivityresult (intRequestcode,intResultCode, Intent data) {String result=data.getextras (). getString ("result"));//get the data returned after the new activity is closed }}
When the new activity is closed, the data returned by the new activity is passed through Intent , and theAndroid platform invokes the previous activity 's Onactivityresult () method, the Intent that holds the returned data is passed in as the third input parameter, and the third input parameter in the Onactivityresult () method can be used to remove a new The data returned by the Activity.
The function of the request code:
use Startactivityforresult (intent Intent. Intrequestcode activity,
used to identify the request source. For example: an activity with two buttons, click on both buttons will open the same activity, whether it is the button to open the new activity, when the new when activity is closed, the system invokes the previous activity 's onactivityresult (int requestcode, int resultcode, Intent data) method. In the Onactivityresult () method, you can do this if you need to know that the new Activity is opened by that button and you want to do the appropriate business processing:
@Override Public voidonCreate (Bundle savedinstancestate) {... button1.setonclicklistener (Newview.onclicklistener () { /c3> Public voidOnClick (View v) {startactivityforresult (new Intent (mainactivity). This, Newactivity.class),1); }}); Button2.setonclicklistener (Newview.onclicklistener () { Public voidOnClick (View v) {startactivityforresult (new Intent (mainactivity). This, Newactivity.class),2); }}); @Overrideprotectedvoid Onactivityresult (intRequestcode,intResultCode, Intent data) { Switch(requestcode) { Case 1: //request from button 1 for appropriate business processing Case 2: //request from button 2 for appropriate business processing } }
The function of the result code: used to indicate the source of the returned result.
In aActivity, you may use thestartactivityforresult () method to open several different activity handle different business when these new activity when off, the system calls the front activityonactivityresult (int requestcode, int resultcode, intentdata) method. In order to know which new activityonactivityresult () method can do this (Resultactivityactivity) :
Public classresultactivity extends Activity {..... Resultactivity. This. Setresult (1, intent); Resultactivity. This. Finish (); Public classnewactivity extends Activity {... Newactivity. This. Setresult (2, intent); Newactivity. This. Finish (); Public classMainactivity extends Activity {//in the activity will open resultactivity and newactivity@OverrideprotectedVoidonactivityresult (intRequestcode,intResultCode, Intent data) { Switch(resultcode) { Case 1: //return data for resultactivity Case 2: //return data for newactivity } }}
If you need to return data or results, use Startactivityforresult (Intent Intent, Intrequestcode), Requestcode and receive the returned data/ result handler function is Onactivityresult (intrequestcode, int resultcode, Intent data), here's Requestcode Is the Startactivityforresult Requestcode,resultcode is setresult inside of the ResultCode, the data returned in the
Android Onactivityresult and Setresult