Rajesh J
Rajesh J

Reputation: 91

How to call onActivityResult for fragment

I have this code

Intent intent = new Intent(getActivity(), PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
getActivity().startActivityForResult(intent, paypal.REQUEST_CODE_PAYMENT);

override function call onActivityResult but it call the result.

Upvotes: 0

Views: 577

Answers (3)

Paul
Paul

Reputation: 970

Where do you override the onActivityForResult method?.

If the answer is in your FragmentActivity. You should use: getActivity().startActivityForResult

If the answer is in your Fragment. You should use: startActivityForResult.

Upvotes: 0

erakitin
erakitin

Reputation: 11517

If you call startActivityForResult from the Fragment the result will deliver to the Fragment. And if you call startActivityForResult from the Activity the result will deliver to the Activity. When Fragment calls StartActivityForResult the requestCode will be changed by the Activity, so it will know how to deliver the result to which Fragment.

In your case you should change line:

getActivity().startActivityForResult(intent, paypal.REQUEST_CODE_PAYMENT);

to:

startActivityForResult(intent, paypal.REQUEST_CODE_PAYMENT);

Upvotes: 1

Ajit Pratap Singh
Ajit Pratap Singh

Reputation: 1289

Do not use getActivity().startActivityForResult(intent, paypal.REQUEST_CODE_PAYMENT);

This calls onActivityResult in the activity

Instead use the method in fragment

startActivityForResult(intent, paypal.REQUEST_CODE_PAYMENT);

Upvotes: 1

Related Questions