Recently while working with tabs, I see this situation onActivityResult , always getting 0(RESULT_CANCELLED).
I have four activities A-Activity,B-TabActivity,C-Activity, D-Activity. where A starts TabActivity B, and in this TabActivity, Im displaying 2 tabs C and D as child activities.
I need result from C or D whether success or not, when Activity is closed. So I started TabActivity B which displays C and D as tabs.. as shown below,
Intent intent = new Intent(this, TabActivityB.class);
startActivityForResult(intent,REQUEST_CODE_PAYMENT);
now after getting required information in Activity C, I closed Activity C by setting result as below
Intent data = new Intent();
setResult(Activity.RESULT_OK,data);
finish();
and when i check the resultCode in Activity A, in
onActivityResult(int requestCode, int resultCode, Intent data)
method, Im always getting
0(RESULT_CANCELLED)
Solution to handle this onActivityResult issue,
In child Activity C and D, before finish we should check the getParent() method null or not… and then setResult accordingly… as shown below,
Intent data = new Intent();
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
finish();
After applying this change, Im able to get the exact resultCode in Activity A.
Hope it helps somebody..
Cheers 🙂
Ref link & credits:http://stackoverflow.com/a/2621390/341443
You may also interested in
- Unable to resolve superclass of Activity
- Get a Dialog style activity window to fill the screen
- Display Menu items in ActionBar when using Fragment