使用 startActivityForResult,如何在子活动中获取 requestCode?

我有四个活动,比如 A,B,C 和 D。 我的情况是 A 将通过 startActivityForResult 启动活动 B。

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

在其他情况下,我会 B 与其他情况。喜欢

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

在 B 中,我需要根据 requestCode 调用 C 或 D。也就是说,如果一个人需要启动 C else D。
因此,我需要知道如何检查子活动(这里是 B)中的 requestCode。
换句话说,我想得到活动 B 开始时使用的请求代码。

77848 次浏览

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

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.

I ended up using custom Intent action to pass this kind of information to the launching Activity.

protected static final String ACTION_DO_C = "do_c";
protected static final String ACTION_DO_D = "do_d";

Then you'd go like:

final Intent intent = new Intent(this,B.class)
intent.setAction(ACTION_DO_C);
startActivityForResult(intent,ONE);

And in Activity B you get the action easily:

getIntent().getAction();

You can use getCallingActivity() to get the activity that started current activity and that will receive the result value with response code at the end.