Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 18.104.22.168 release ready!
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.
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
The resultCode is used by the destination activity to flag to its source activity what it status (e.g. OK, Cancel etc).
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.
Initializing and starting the second Activity and Call Back method to get the message from the second Activity
ActivityTwo will perform its job and after that, it returns to its caller activity.
You can also send result without any data using setResult(int resultCode)
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.