简体   繁体   中英

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.

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);

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM