By using startActivityForResult(Intent intent, int requestCode)
you can start another Activity
and then receive a result from that Activity
in the onActivityResult(int requestCode, int resultCode, Intent data)
method. The result will be returned as an Intent
. An intent can contain data via a Bundle
In this example MainActivity
will start a DetailActivity
and then expect a result from it. Each request type should have its own int
request code, so that in the overridden onActivityResult(int requestCode, int resultCode, Intent data)
method in MainActivity
, it can be determined which request to process by comparing values of requestCode
and REQUEST_CODE_EXAMPLE
(though in this example, there is only one).
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9345;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new instance of Intent to start DetailActivity
final Intent intent = new Intent(this, DetailActivity.class);
// Start DetailActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
// onActivityResult only get called
// when the other Activity previously started using startActivityForResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the DetailActivity
// By convention RESULT_OK means that whatever
// DetailActivity did was executed successfully
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(DetailActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// setResult wasn't successfully executed by DetailActivity
// Due to some error or flow of control. No data to retrieve.
}
}
}
}
public class DetailActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
final Button button = (Button) findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new Intent object as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode as Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the DetailActivity to
// return back to MainActivity
finish();
}
});
}
@Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// as Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
Data is only returned once you call finish()
. You need to call setResult()
before calling finish()
, otherwise, no result will be returned.
Make sure your Activity
is not using android:launchMode="singleTask"
, or it will cause the Activity
to run in a separate task and therefore you will not receive a result from it. If your Activity
uses singleTask
as launch mode, it will call onActivityResult()
immediately with a result code of Activity.RESULT_CANCELED
.
Be careful when using android:launchMode="singleInstance"
. On devices before Lollipop (Android 5.0, API Level 21), Activities will not return a result.
startActivityForResult()
. When starting one of your own activities to receive a result, you should use an explicit intent to ensure that you receive the expected result.
An explicit intent
is always delivered to its target, no matter what it contains; the filter
is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.