Android Onactivityresult and Setresult methods
Recently do company project encounter Android Onactivityresult and Setresult, in the process of the process error, it is necessary to record, so as not to use error again.
If you want to get the data returned after the new open activity closes in the activity, you need to use the Startactivityforresult provided by the system (Intent intent,int Requestcode) Method opens a new activity, the new activity is closed and the data is returned to the previous activity, and in order to get the returned data, you must rewrite the Onactivityresult in the previous activity (int requestcode, int Resultcode,intent data) Method:
public class Mainactivity extends activity {
@Overrideprotected void onCreate (Bundle savedinstancestate) {
Button button = (button) This.findviewbyid (R.id.button);
Button.setonclicklistener (New View.onclicklistener () {
//Click the button to open a new activity
publicvoid OnClick (View v) {
//The second parameter is the request code, which can be numbered according to the business requirements
Startactivityforresult (new Intent (Mainactivity.this, Newactivity.class), 1;
});
}
The first parameter is the request code, that is, the call to Startactivityforresult () passes past values
//The second parameter is the result code, which is used to identify which new activity the returned data comes from
@Override protected Voidonactivityresult (int requestcode, int resultcode, Intent data) {
String result =data.getextras (). GetString ("result"))//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 the Android platform invokes the Onactivityresult () method of the preceding activity, The intent that holds the returned data is passed as a third input parameter, and the third input parameter in the Onactivityresult () method can be used to fetch the data returned by the new activity.
The function of the request code:
Use Startactivityforresult (Intent Intent. Intrequestcode) method to open a new activity, we need to pass a request code for the method. The value of the request code is set by itself based on the business needs,
used to identify the source of the request. For example: An activity has two buttons, and clicking on both buttons will open the same activity, whether that button opens a new activity, and when the new activity closes, The system invokes the Onactivityresult (int requestcode, int resultcode, Intent data) method of the preceding activity. In the Onactivityresult () method, you can do this if you need to know that the new activity is opened by that button and that you want to make the appropriate business process:
@Override public
void OnCreate (Bundle savedinstancestate) {
....
Button1.setonclicklistener (Newview.onclicklistener () {public
void OnClick (View v) {
Startactivityforresult (Newintent (Mainactivity.this, Newactivity.class), 1);
});
Button2.setonclicklistener (Newview.onclicklistener () {public
void OnClick (View v) {
Startactivityforresult (Newintent (Mainactivity.this, Newactivity.class), 2);
});
@Override protected Voidonactivityresult (int requestcode, int resultcode, Intent data) {
switch (requestcode) { Case
1:
//From button 1 request, the corresponding business processing case
2:
//From button 2 request, for the corresponding business processing
}
}
The effect of the result code: used to indicate the source of the returned results.
In an activity, it is possible to use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new operations are closed, The system invokes the Onactivityresult (int requestcode, int resultcode, Intentdata) method of the preceding activity. In order to know which new activity the returned data comes from, this can be done in the Onactivityresult () method (Resultactivity and newactivity for the new activity to be opened):
public class Resultactivity extends activity {
..... ResultActivity.this.setResult (1, intent);
ResultActivity.this.finish ();
}
public class Newactivity extends activity {
..... NewActivity.this.setResult (2,intent);
NewActivity.this.finish ();
}
public class Mainactivity extends activity {//in which the activity will open resultactivity and newactivity
@Override protected Voidonactivityresult (int requestcode, int resultcode, Intent data) {
switch (resultcode) {case
1:
// Resultactivity return data for Case
2:
//newactivity return Data
}
}}
If you need to return data or results, use startactivityforresult (Intent Intent, Intrequestcode), andthe Requestcode value is custom, The target activity used to identify the jump.
The target activity of the jump is to return the data/result,setresult (int resultcode) returns only the result without data, or setresult (int resultcode, Intent data) Both of them back! The processing function for receiving the returned data/results is onactivityresult (intrequestcode, int resultcode, Intent data), where the Requestcode is Startactivityforresult's Requestcode, ResultCode is Setresult inside ResultCode, the returned data inside
Thank you for reading, I hope to help you, thank you for your support for this site!