36

onActivityResult() is not getting called. Below is my code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to

    Log.e("CALLED", "OnActivity Result");

    if (requestCode == TEAM_SELECTED_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            try {
                 mySelectedTeam = getIntent().getStringExtra("teamName");
                txtSelectTeamCreateMatch.setText(mySelectedTeam);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Here's I'm starting the SelectTeamActivity:

Intent intent=new Intent(CreateMatch.this,SelectTeamActivity.class);
startActivityForResult(intent, TEAM_SELECTED_REQUEST_CODE);
//overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);

Intent intent = getIntent();
intent.putExtra("teamID", teamDataList.get(position).getTeamId().toString());
intent.putExtra("teamName", teamDataList.get(position).getTeamName().toString());
setResult(1, intent);
Sufian
  • 6,405
  • 16
  • 66
  • 120
Parin Parikh
  • 775
  • 1
  • 6
  • 17

10 Answers10

88

Option 1 :

If you're calling startActivityForResult() from the Fragment then you should call startActivityForResult() and not getActivity().startActivityForResult(), as it will result in Fragment's onActivityResult().

If you're not sure where you're calling on startActivityForResult() and how you will be calling methods.

Option 2:

Since Activity gets the result of onActivityResult(), you will need to override the Activity's onActivityResult() and call super.onActivityResult() to propagate to the respective Fragment for unhandled results codes or for all.

If above 2 options do not work, then refer option 3 as it will definitely work.

Option 3 :

Explicit call from Fragment to onActivityResult() function as follows

In parent Activity class, override the onActivityResult() and even override the same in Fragment class and call as the following code.

In parent class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
    fragment.onActivityResult(requestCode, resultCode, data);
}

In child class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   //in fragment class callback
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
  • 4
    if you satisfied with my answer,can you please accept it? – Akash Moradiya Nov 18 '14 at 07:00
  • @AkashMoradiya, What to do if all 3 didn't work? 2nd one worked when I was using `GoogleSignIn`, but none works when I create intent. This happens within the same fragment (GoogleSignIn works but not the intent). I overrode both parent Activity function and the fragment's one – Kiura Nov 12 '19 at 08:54
  • doesn't work for me. Tried option 1 and option 2. – Qiang Zhang Dec 27 '21 at 04:50
16

I solved my problem by removing android:noHistory="true" from AndroidManifest.xml.

torchhound
  • 510
  • 6
  • 15
Android Dev
  • 1,496
  • 1
  • 13
  • 25
8

I had same issue. My calling activity was 'singleInstance'. Removing that solved my issue.

spgodara
  • 984
  • 9
  • 10
  • 1
    Thank you, you're great! It also behaves differently on different API levels. KitKat didn't like my result activity being singleInstance, but Android N didn't mind at all. Really strange. – Aleks N. Aug 22 '16 at 13:57
  • 1
    @ClarenceLeung What the answer says basically. Instead of making it `singleInstance` make it `singleTop` or `standard`. – Aleks N. Oct 10 '16 at 08:41
  • wasn't working for me because my activity was `singleTask`. removing it solved my issue as well. – shoe Nov 27 '16 at 15:12
  • Thanks, man! Looks like it is not working with `singleInstance` since Android M. – Daniels Šatcs Sep 09 '17 at 22:05
  • it cannot work with `singleInstance` because it starts a new Task and within it there is no Activity to go back to – NikkyD May 28 '18 at 16:31
7

onActivityResult called but using wrong intent reference to get data from result intent :

getIntent().getStringExtra("teamName")

Replace with this :

data.getStringExtra("teamName")

Here data is result intent.

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
5

I had similar problem and my issue was about requestCode parameter which was set to -1 in startActivityForResult method. So when I changed requestCode value to 1 or 0 onActivityResult started getting called.

Arsenius
  • 4,972
  • 4
  • 26
  • 39
  • Thank you; this was exactly my problem. The docs are really unclear on this; they say "`requestCode`: int: If >= 0, this code will be returned in onActivityResult() when the activity exits" - for this reason I deliberately set the `requestCode` to -1 as I don't need it to be returned to the `onActivityResult`, but in fact the `onActivityResult` does not even get called in this case, which is really not clear from the docs – Adam Burley Jun 18 '21 at 14:50
4

I just had the same issue and I'm surprised none of the solutions I've looked at have mentioned the following:

The value of the constant RESULT_OK is -1, and if you give the value of 1 in the method setResult, then the resultcode wouldn't be equal to the RESULT_OK and the condition wouldn't get processed. I hope someone finds this helpful.

Harsha
  • 1,568
  • 2
  • 19
  • 22
2

After reviewing pradip tilala's answer, I have solved my problem by removing below line from AndroidManifest.xml

android:noHistory="true"
Pooja
  • 475
  • 5
  • 14
1

I had the same issue and I solved that by adding these parameters to activity:

AlwaysRetainTaskState = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop
Masoud
  • 35
  • 1
  • 8
1

After referring answer of @spgodara.

I have below AndroidManifest.xml for my SecondActivity class:

<activity android:name=".SecondActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@style/AppThemeHome"
        android:windowSoftInputMode="adjustResize"/>

I have put simple Toast to find when callback onActivityResult of MainActivity is being called. I found that is just called on create of SecondActivity (On opening of the SecondActivity). It's working fine on Redmi Note 3 device with Marshmallow, but not working on Kitkat device.

After removing below the line from AndroidManifest.xml deceleration mentioned above:

android:launchMode="singleTask"

and calling intent like below worked for me:

 Intent intent = new Intent(this, SecondActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);
Pradip Tilala
  • 1,715
  • 16
  • 24
1

Removed this line and its working fine.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Thanks for hint.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437