Parin Parikh
Parin Parikh

Reputation: 785

onActivityResult() not called

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

Upvotes: 37

Views: 52688

Answers (10)

Pooja
Pooja

Reputation: 485

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

android:noHistory="true"

Upvotes: 3

Pratik Butani
Pratik Butani

Reputation: 62421

Removed this line and its working fine.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Thanks for hint.

Upvotes: 2

Pradip Tilala
Pradip Tilala

Reputation: 1813

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

Upvotes: 1

Arsenius
Arsenius

Reputation: 5652

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.

Upvotes: 6

Hardik Lakhani
Hardik Lakhani

Reputation: 1544

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

Upvotes: 17

Masoud
Masoud

Reputation: 35

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

AlwaysRetainTaskState = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop

Upvotes: 1

Harsha
Harsha

Reputation: 1746

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.

Upvotes: 4

spgodara
spgodara

Reputation: 1084

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

Upvotes: 8

Akash Moradiya
Akash Moradiya

Reputation: 3322

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
}

Upvotes: 87

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

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.

Upvotes: 7

Related Questions