First, if you want to get the data returned after the closure of the newly opened Activity in Activity, you need to use the Start Activity ForResult (Intent intent, int request Code) method provided by the system to open the new Activity. After the closure of the new Activity, the data will be returned to the front Activity. In order to get the returned data, you must rewrite the onActivity Result (int request Code) in the previous Activity. 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) {
//Get the data returned when the newly opened Activity is closed
//The second parameter is the request code, which can be numbered according to business requirements.
startActivityForResult(new Intent(MainActivity.this, OtherActivity.class), 1);
}
});
}
/** * In order to get the returned data, the onActivityResult method must be overridden in the previous Activity (referring to the MainActivity class) * * requestCode Request code, which calls startActivityForResult() to pass past values * resultCode Result code, which identifies which new Activity the returned data comes from */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getExtras().getString("result");//Get the data returned after the closure of the new Activity Log.i(TAG, result); }
}
When the new Activity is closed, the data returned by the new Activity is transferred through Intent. The android platform calls the onActivityResult() method of the previous Activity, and the Intent storing the returned data is passed in as the third input parameter. The data returned by the new Activity can be retrieved by using the third input parameter in the onActivityResult() method.
2. Open a new Activity by using the startActivity ForResult (Intent intent, int request Code) method. Before closing the new Activity, it needs to return data to the front Activity. It needs to be implemented by using the setResult(int resultCode, Intent data) method provided by the system:
Write code slices here.
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();
//Store the returned data in Intent
intent.putExtra("result", "My name is linjiqin");
//Setting 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. The RESULT_OK used in the above code is a constant defined by the System Activity class with a value of -1. 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;
}
Operation result
Explanation: When you click on the "Open New Activity" button, you will jump to the "I am the newly opened Activity" page.
When you click the "Close" button, close the current page, and jump to the "I am the old Activity" page, and pass the result parameter to the previous Activity.
The Role of Request Codes
To open a new Activity using the startActivityForResult (Intent intent, int request Code) method, we need to pass in a request code (the second parameter) for the startActivityForResult() method. The value of the request code is set by itself to identify the source of the request according to the business needs. For example, an Activity has two buttons. Clicking on these two buttons will open the same Activity. Whether that button opens the new Activity, when the new Activity is closed, the system will call the onActivity Result (int request Code, int result Code, Intent data) method of the previous Activity. In the onActivityResult() method, if you need to know that the new Activity is opened by that button and do the corresponding business processing, you can do this:
@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: //Requests from button 1 are processed accordingly case 2: //Requests from button 2 for corresponding business processing } }
}
The Role of Result Codes
In an Activity, you may use the startActivityForResult() method to open multiple different activities to handle different businesses. When these new activities are closed, the system will call the onActivityResult (int request Code, int resultCode, Intent data) method of the previous Activity. In order to know which new Activity the returned data comes from, you can do this in the onActivityResult() method (ResultActivity and NewActivity are new activities 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 this Activity, ResultActivity and New Activity are opened
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode){
case 1:
// ResultActivity's return data
case 2:
// Return data of New Activity
}
}
}