onActivityResult() not called

£可爱£侵袭症+ 提交于 2019-12-21 06:47:13

问题


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

回答1:


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.




回答2:


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
}



回答3:


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




回答4:


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




回答5:


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.




回答6:


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

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



回答7:


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



回答8:


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.




回答9:


Removed this line and its working fine.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Thanks for hint.




回答10:


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

android:noHistory="true"


来源:https://stackoverflow.com/questions/26988038/onactivityresult-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!