Updated 11 October 2021
In android, we all know about Activity which acts as a container for other UI components and is most commonly used.
There is a scenario when two activity depends on each other. The first activity requires some data from the second Activity, in that case, it doesn’t need to be a one-way operation.
You can start another activity and receive a result back. For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the Contacts app in order for the user to select a contact and you’ll receive the contact details as a result.
With the help of the android startActivityForResult() method, we can get the result from another activity. The android startActivityForResult method requires a result from the second activity (activity to be invoked).
In such a case, we need to override the onActivityResult method that is invoked automatically when the second activity returns the result.
The entire startActivityForResult, and onActivityResult is allowing a 2-way communication between the source activity and the destination activity.
The source activity call, startActivityForResult by sending in the intent together with the requestCode to Android SDK.
Android SDK then opens the activity accordingly as stated in the Intent.
1 2 |
Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(REQUEST_CODE, intent);// Activity is started with particular requestCode |
Once the destination activity has finished with its job, it returns to its caller activity. It could send the result back using setResult by sending in the resultCode and intent
1 2 3 4 5 6 |
private void closeActivity() { Intent intent=new Intent(); intent.putExtra("MESSAGE",message); //passing data to calling activity setResult(REQUEST_CODE,intent); finish();//finishing activity } |
The resultCode is used by the destination activity to flag to its source activity what it status (e.g. OK, Cancel etc).
1 2 3 4 5 6 7 8 9 10 |
// 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 if (requestCode == REQUEST_CODE && resultCode == RESULT_CODE){ String message=data.getStringExtra("MESSAGE"); // TODO: Do something with your extra data } } |
The requestCode is used by the source activity to know which destination activity is returning the call.
We could see that the destination activity has no visibility of requestCode
Like Getting a result from another Activity you need to call the Fragment’s method startActivityForResult(Intent intent, int requestCode). note that you should not call getActivity().startActivityForResult() as this will take the result back to the Fragment’s parent activity.
Receiving the result can be done using the Fragment’s method onActivityResult(). You need to make sure that the Fragment’s parent Activity also overrides onActivityResult() and calls its super implementation.
In the following example, ActivityOne contains FragmentOne, which will start ActivityTwo and expect a result from it.
You must override this method as the second Activity will always send its results to this Activity and then to the Fragment.
1 2 3 4 |
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } |
Initializing and starting the second Activity and Call Back method to get the message from the second Activity
1 2 3 4 |
private void startSecondActivity() { Intent intent = new Intent(getActivity(), ActivityTwo.class); startActivityForResult(REQUEST_CODE, intent); } |
1 2 3 4 5 6 7 8 |
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE && resultCode == RESULT_CODE) { String testResult = data.getStringExtra(MESSAGE); // TODO: Do something with your extra data } } |
ActivityTwo will perform its job and after that, it returns to its caller activity.
1 2 3 4 5 6 |
private void closeActivity() { Intent intent = new Intent(); intent.putExtra(MESSAGE, "message data back to ActivityOne"); setResult(RESULT_CODE, intent); finish(); } |
You can also send result without any data using setResult(int resultCode)
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.