To get data from our other activity (Say SecondActivity) that is opened by an activity(Say MainActivity), use startActivityForResult()
. SecondActivity send back result via intent. While sending back intent it will set the response using setResult()
- RESULT_OK : Result send is OK.
- RESULT_CANCELED : Activity cancel to send result.
Finally in MainActivity class will get result in onActivityResult()
method and check the request code and result code.
//****** File : MainActivity.java ****** // Start second activity. 2 is request code Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, 2); // Call Back method to get the Message form other Activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Check if the request code is same as what is passed here it is IntentId if (requestCode == IntentId) { if (resultCode == RESULT_OK) { // Result code is OK String message = data.getStringExtra("message"); } else if (resultCode == RESULT_CANCELED){ // Handle failure case } } } //****** File : SecondActivity.java ****** Intent intent = new Intent(); // Put Message to pass over intent intent.putExtra("message",message); // Set result OK setResult(RESULT_OK,intent); // Set result CANCEL setResult(RESULT_CANCELED, intent);
Example
Create a MainActivity, and launch another activity for the result.
public class MainActivity extends AppCompatActivity { public static final int ASK_REQUEST =1; private static Button getMessageBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getMessageBtn = (Button)findViewById(R.id.secondActivityBtn); getMessageBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Start SecondActivity with the request code Intent in = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(in, ASK_REQUEST); } }); } // Call Back method to get the Message form other Activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Check if the request code is same as what is passed here it is ASK_REQUEST if (requestCode == ASK_REQUEST) { if (resultCode == RESULT_OK) { // Result code is OK, get String extra and set message String message = data.getStringExtra("message"); } else if (resultCode == RESULT_CANCELED){ } } } }
Second activity handles the intent, process request and send back the response to the calling activity.
public class SecondActivity extends AppCompatActivity implements OnClickListener{ private static Button cancel, submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); cancel = (Button)findViewById(R.id.cancel); submit = (Button)findViewById(R.id.submit); cancel.setOnClickListener(this); submit.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.submit: { // Set the resultCode to Activity.RESULT_OK to indicate a success and attach the Intent // which contains result data Intent intent = new Intent(); intent.putExtra("message", "Hello World"); setResult(RESULT_OK, intent); // Close the SecondActivity return to MainActivity finish(); break; } case R.id.cancel: { Intent intent = new Intent(); setResult(RESULT_CANCELED, intent); finish(); break; } } } }