onActivityResult never called

2019-01-25 05:29发布

问题:

So far, I used the startActivity function with success and now that I need to use the startActivityResult, I have a problem.

When using this function, the activity I expect to be launched is launched correctly but it seems like the onActivityResult function is never called.

Here is my code in the FriendPicker activity, to lauch the MoodPicker activity:

Intent intent = new Intent(FriendPicker.this, MoodPicker.class);
startActivityForResult(intent, 2);

And here is my code in the MoodPicker activity:

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

    Log.i("in OnActivityResult", "Activity Result");                        

    switch (requestCode)
    {
        case 2:             
            Log.i("in OnActivityResult", "Activity Resut 2");                

            break;
    }
}

and nothing in my logfile is written (I of course, checked that my log file was working properly and double check with a text view output).

Is there something I forgot to declare in the Manifest file?

Thanks in advance.

回答1:

Did you add the setResult() call in your MoodPicker class ?



回答2:

I had same problem and solved it: Just remove

android:launchMode="singleInstance"


回答3:

In my case, I didn't realize I was calling startActivityForResult from an activity that had the android:noHistory attribute set to true in the manifest. Therefore, the onActivityResult was never called as there was no activity instance anymore.



回答4:

If I am reading this right, all the code referenced needs to be in "FriendPicker". In "MoodPicker" you need code like this that sets the result and ends itself:

this.setResult(SUCCESS_RETURN_CODE, i);
this.finish();

Let me know if this helps...



回答5:

android:noHistory="true" 

like

android:launchMode="singleInstance"

will stop onActivityResult from receive result.



回答6:

There is bug in android API. In startActivityForResult(intent, requestCode); This funktion does work as long as requestCode = 0. However, if you change the request code to anything other than zero, the ApiDemos will fail (and OnActivityResult won't be called). Found here:

[EDIT: Link removed as google group overrun with spam]



回答7:

FriendPicker activity

Intent intent = new Intent(FriendPicker.this, MoodPicker.class);
startActivityForResult(intent, 2);


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

    Log.i("in OnActivityResult", "Activity Result");                        

    switch (requestCode)
    {
        case 2:             
             if (resultCode == Activity.RESULT_OK) {     //optional   
            Log.i("in OnActivityResult", "Activity Resut 2");                
              }
            break;
    }
}

MoodPicker class

Intent intent = new Intent(MoodPicker.this, FriendPicker.class);
        setResult(Activity.RESULT_OK, intent);
finish();

I had the same problem using onActivityResult(); cause i didn´t understand how this will be correctly applied, here you can find a good explanation how to use onActivityResult onActivityResult doesn't work?



回答8:

The mistake that I had made was that after creating my Intent I was calling startActivity() instead of startActivityForResult()

Sometimes the simple ones kill you :)



回答9:

My silly mistake. Might help someone.

I kept onActivityResult(...) in another class. It has to be as a method in the class where the startActivityForResult() is called.