Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using startActivityForResult, how to get requestCode in child activity?

I have four activities, say A, B, C and D. My situation is A will start the activity B by startActivityForResult.

startActivityForResult(new Intent(this,B.class),ONE); 

In other situation i will B with other situation. like

 startActivityForResult(new Intent(this,B.class),TWO); 

In B, I need to call C or D depending on requestCode. I.e if ONE need to start C else D.
So I need to know how to check the requestCode in the child Activity (B here).
In other words, I want to get the request code that Activity B was started with.

like image 771
Jithin Avatar asked Feb 24 '11 11:02

Jithin


People also ask

How can I get result from startActivityForResult?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

What is requestCode in startActivityForResult?

The requestCode helps you to identify from which Intent you came back. For example, imagine your Activity A (Main Activity) could call Activity B (Camera Request), Activity C (Audio Recording), Activity D (Select a Contact).

How do you send data into activity results?

Put the data that you want to send back to the previous activity into an Intent . The data is stored in the Intent using a key-value pair. Set the result to RESULT_OK and add the intent holding your data. Call finish() to close the Second Activity.

How will you get the data in second activity?

We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.


2 Answers

You can pass request code by put extra.

intent.putExtra("requestCode", requestCode);    

Or if you have used startActivityForResult many times, then better than editing each, you can override the startActivityForResult in your Activity, add you code there like this

@Override     public void startActivityForResult(Intent intent, int requestCode) {         intent.putExtra("requestCode", requestCode);         super.startActivityForResult(intent, requestCode);     } 

So there is no need to edit all your startActivityForResult
Hope it helped you

like image 94
Labeeb Panampullan Avatar answered Oct 05 '22 13:10

Labeeb Panampullan


The request code is not passed to the started activity automatically because it doesn't (and shouldn't) need to know this value. It only needs to know what to do and not where it was started from.

Starting an activity is really just another form of calling a method. When you call a method, you receive the result synchronously (right there where you made the call). In this case you are only passing in the information that method needs to do its work. You are not telling it where you called it from.

Starting an activity is the asynchronous analog of calling a method, in which case you receive the result in the special method onActivityResult(). In this method, you need to know what to do with the result you just received and you have the request code for this.

To make it a bit clearer why it isn't a good idea to pass the request code as a parameter, consider the example activity which is showing a product you can buy. On this activity there are two buttons labeled "Buy" and "Login" (as you are currently not logged in). Pressing "Login" will start an activity named "Login" which will try to log in the user using the provided information. Pressing "Buy" will first start the very same "Login" activity and if the login was successful, start the buy activity.

Now, the "Login" button uses request code 1 to start the login activity, but the "Buy" button can't use the same request code as it will have to do something different if the login is successful. So, the "Buy" button uses request code 2.

In the "Login" activity you might receive two different request codes depending on where it was called from, but you will need to do the very same procedure.

So, if you pass in the request code as a parameter, you will end up with code that needs to do the same stuff for a couple of different request codes, like:

if (requestCode == LOGIN || requestCode == BUY) {     // ... } else ... 

You will also end up with storing the request code constants in a central location e.g. a class named RequestCodes.

In short, the request code should only be used to decide what to do with the received result. This way you will end up with a more modular, easier to maintain and easier to extend code.

like image 39
Szabolcs Berecz Avatar answered Oct 05 '22 13:10

Szabolcs Berecz