onActivityResult() not called
Asked Answered
D

10

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);
Displant answered 18/11, 2014 at 6:31 Comment(0)
C
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.

Colicroot answered 18/11, 2014 at 6:40 Comment(0)
S
87

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
}
Specs answered 18/11, 2014 at 6:34 Comment(3)
if you satisfied with my answer,can you please accept it?Specs
@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 oneDeragon
doesn't work for me. Tried option 1 and option 2.Posterity
M
17

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

Merciful answered 30/1, 2017 at 12:4 Comment(1)
I don't even have android:noHistory="true" in AndroidManifest.xmlPosterity
B
8

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

Barrack answered 8/8, 2015 at 6:22 Comment(5)
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.Gould
@ClarenceLeung What the answer says basically. Instead of making it singleInstance make it singleTop or standard.Gould
wasn't working for me because my activity was singleTask. removing it solved my issue as well.Hydrolysate
Thanks, man! Looks like it is not working with singleInstance since Android M.Theological
it cannot work with singleInstance because it starts a new Task and within it there is no Activity to go back toMeteor
C
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.

Colicroot answered 18/11, 2014 at 6:40 Comment(0)
G
6

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.

Genethlialogy answered 14/3, 2019 at 9:23 Comment(1)
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 docsHousum
M
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.

Morphophoneme answered 24/1, 2017 at 20:22 Comment(2)
Thanks very much, exactly that was my problem.Cranston
Hmm, we usually call setResult(Activity.RESULT_OK, data), so hardly can face this situation.Dreamland
F
3

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

android:noHistory="true"
Furan answered 10/9, 2019 at 17:6 Comment(0)
S
1

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

AlwaysRetainTaskState = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop
Startling answered 20/9, 2017 at 9:23 Comment(0)
H
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);
Haftarah answered 4/6, 2019 at 5:40 Comment(0)
M
1

Removed this line and its working fine.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Thanks for hint.

Mong answered 16/7, 2019 at 12:58 Comment(1)
this should be the answer. verified in github.com/zhangtemplar/SMSForwardPosterity

© 2022 - 2024 — McMap. All rights reserved.