First, 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, must be overridden in the previous activity onactivityresult (int requestcode, int ResultCode, Intent data) method.
Package com.ljq.activitys;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
public class Mainactivity extends Activity {
Private final static String tag= "Mainactivity";
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button btnopen= (button) This.findviewbyid (R.id.btnopen);
Btnopen.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Gets the data returned after the new open activity is closed
The second parameter is the request code, can be numbered according to business requirements
Startactivityforresult (New Intent (Mainactivity.this, Otheractivity.class), 1);
}
});
}
/**
* To obtain the data returned, the Onactivityresult method must be overridden in the previous activity (referring to the Mainactivity class)
*
* Requestcode The request code, that is, call Startactivityforresult () pass the past value
* ResultCode result code, which is used to identify which new activity the returned data came from
*/
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
String result = Data.getextras (). getString ("result");//The data returned after the new activity has been closed
LOG.I (TAG, result);
}
}
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 previous activity. The intent that holds the returned data is passed in as a third input parameter, and the third input parameter in the Onactivityresult () method allows you to remove the data returned by the new activity.
Second, use Startactivityforresult (Intent Intent, int requestcode) method to open a new activity, The need to return data to the previous activity before the new activity is closed needs to be implemented using the system-provided setresult (int resultcode, Intent data) method:
Package com.ljq.activitys;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
public class Otheractivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.other);
Button btnclose= (button) Findviewbyid (r.id.btnclose);
Btnclose.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Data is returned using intent
Intent Intent = new Intent ();
Depositing return data into intent
Intent.putextra ("Result", "My name is Linjiqin");
Set the return data
OtherActivity.this.setResult (RESULT_OK, intent);
Close activity
OtherActivity.this.finish ();
}
});
}
}
The first parameter value of the Setresult () method can be defined according to business needs, and the RESULT_OK used in the code above is a constant defined by the system activity class, with a value of 1, and the code snippet is as follows:
public class Android.app.Activity extends ... {
public static final int result_canceled = 0;
public static final int result_ok =-1;
public static final int result_first_user = 1;
}
Run results
Note: When you click on the "Open New Activity" button, you will be redirected to the "I am the newly opened Activity" page;
When the "Close" button is clicked, the current page is closed and the "I am old Activity" page is transferred, and the result parameter is passed to the previous activity
function of the request code
Using the Startactivityforresult (Intent Intent, int requestcode) method to open a new activity, we need to pass in a request code for the Startactivityforresult () method ( Second parameter). The value of the request code is self-set according to the business need and is used to identify the source of the request. 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 activity is closed, the system will call the previous activity 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 void OnCreate (Bundle savedinstancestate) {
....
Button1.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Startactivityforresult (New Intent (Mainactivity.this, Newactivity.class), 1);
}
});
Button2.setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Startactivityforresult (New Intent (Mainactivity.this, Newactivity.class), 2);
}
});
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Switch (requestcode) {
Case 1:
Request from Button 1 for appropriate business processing
Case 2:
Request from button 2 for appropriate business processing
}
}
}
Effect of the result code
In an activity, you might use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new activity is closed, The system invokes the previous activity's onactivityresult (int requestcode, int resultcode, Intent data) method. In order to know which new activity the returned data comes from, you can do so in the Onactivityresult () method (Resultactivity and newactivity are the new activity to open):
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 the activity will open resultactivity and newactivity
@Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Switch (ResultCode) {
Case 1:
Return data for Resultactivity
Case 2:
Return data for Newactivity
}
}
}
Startactivityforresult Usage Explanation