4

I searched all over and there are similar posts about it, but can't find a solution!

My situation is I have an Activity A that holds a fragment, and from that fragment I want to start a new Activity B that should return some values to the fragment.

On the fragment

startActivityForResult(mapIntent, ConstantsUtils.TOMAP_REQUEST_CODE);

On the Activity B, to return the data

Intent returnIntent = new Intent();
returnIntent.putExtra(SerializationConstants.isSearchSaved, mAbItemsShown.ordinal());
setResult (ConstantsUtils.SUCCESS_RETURN_CODE, returnIntent);
finish();

On the fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

switch (requestCode) {

    case ConstantsUtils.TOMAP_REQUEST_CODE:

            if (resultCode == ConstantsUtils.SUCCESS_RETURN_CODE) {
              //do some stuff 
            }
    }
}

onActivityResult from the fragment is successfully called, with the right requestCode but resultCode is always 0 and intent is always null.

I have no other onActivityResult implementation under Activity A.

In fact, I also try starting the activity from the fragment with getActivity().startActivityForResult(mapIntent, ConstantsUtils.TOMAP_REQUEST_CODE); and implementing onActivityResult on Activity A but it happens the same, right requestCode but wrong resultCode and intent.

I'm using Sherlock Action Bar so my fragment is a SherlockListFragment so I'm using the support library (r18).

Can you help me? Thanks

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
nirvik
  • 386
  • 2
  • 5
  • 18
  • 1
    I don't know what your `super.onActivityResult(requestCode, resultCode, intent);` for. It is not necessary, in my opinion. Also, your request_code need to be >= 0, as it mention [here][doc][doc]: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) – Owen Zhao Aug 29 '13 at 13:41
  • super is not neccessary, i was just trying to see if the problem was because of it, but same result if I don't put it. The constant SUCCESS_RETURN_CODE has a value of 1, but the same happens with Activity.RESULT_OK – nirvik Aug 30 '13 at 07:14
  • Looks like you are calling `finish()` somewhere else in ActivityB. Calling `finish()` without setting the result code and result data would result in exactly the behaviour that you are describing. Either that or ActivityB is crashing, that would do the same thing. – David Wasser Aug 30 '13 at 08:47
  • Then I have to advise you to check the Activity A, in `onStart()` or `onResume` part. If you create a new instances of the fragment in which you called `startActivityForResult`, it would be the problem. Normally, Android will automatically restore the previous state when you activity B call `finish()`. So I you create another fragment instance manually, it will replace the one who get the returned Intent. – Owen Zhao Aug 30 '13 at 10:03

2 Answers2

15

Result code 0 is RESULT_CANCELLED.

The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.

Also the common reason of getting this code is a launching an activity in a new task (check your intent and manifest for flags, which lead to the start of a new task).

Also if you have a parent activity, you should set result code of it instead of set result code of its child (try to getParent and if it is not null, than set result code of it).

esentsov
  • 6,372
  • 21
  • 28
  • I checked and I don't have any special flag in the manifest or when creating the intent. And the value of getParent inside the ActivityB (the one that does the setResult) is null – nirvik Aug 30 '13 at 07:16
  • 3
    Awesome, thanks. The problem for me was that I had launchMode="singleTask" in my manifest. – elimirks Sep 26 '13 at 14:32
  • 1
    @elimirks can you please explain me how you solved your problem? I need to have `launchMode="singleTask"` but I still like to start a new activity. – mariosangiorgio Oct 26 '13 at 09:56
  • 1
    @mariosangiorgio the singleTask launch mode is meant for only stand alone activities. – elimirks Oct 28 '13 at 15:27
8

I spent some time to find out the reason. My mistake was put super.onBackPressed() in the onBackPress() method.

I called finish() in the method. But I think super.onBackPressed() will call the finish() automatically so you will get resultCode 0 always. So just remove the super.onBackPressed() line in the onBackPressed() method.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Harri Westman
  • 140
  • 1
  • 7