Java – startActivityForResult(Intent, int) What is the purpose of integer parameters

startActivityForResult(Intent, int) What is the purpose of integer parameters… here is a solution to the problem.

startActivityForResult(Intent, int) What is the purpose of integer parameters

I’m working on a tutorial that uses the “startActivityForResult” function. I know why we want to use this function, if we want to use the returned data, we use the onActivityResult() function. What I’m wondering is, why do we pass Integer to the startActivityForResult() function? Where is it used?

Thanks for your help!

Solution

When your starting activity completes, the second parameter is passed to the method onActivityResult() that calls the activity. This is to distinguish between different outcomes.

You may encounter a situation where one activity needs to call several other activities to get results, and by specifying a unique int for each activity, you can determine what to do to get the results

EDIT: This is an example.

Let’s say you have a screen (Activity A) where you can select a picture from your gallery and then select it to send to friend. You will launch an activity to select a picture from the gallery by clicking a button, maybe you also have a “friend selector” activity, you can also start with Activity A.

You want both activities to return the data selected by the user. When the user completes the above two activities, onActivityResult() will be called on Activity A. By specifying requestCode for the gallery activity's startActivityForResult(), as well as your “friend selector”, you know what to do in onActivityResult().

Also, note that you do not need to use complex schemes for requestCode. You can pass in values 1 and 2 to the gallery and friend selector, respectively.

Related Problems and Solutions