19

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.

thomaus
  • 6,170
  • 8
  • 45
  • 63

10 Answers10

28

I had same problem and solved it: Just remove

android:launchMode="singleInstance"
ranjeet wagh
  • 306
  • 3
  • 2
23

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.

Xavi Gil
  • 11,460
  • 4
  • 56
  • 71
10

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

Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
9
android:noHistory="true" 

like

android:launchMode="singleInstance"

will stop onActivityResult from receive result.

Nick Jian
  • 457
  • 4
  • 12
6

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...

Lumis
  • 21,517
  • 8
  • 63
  • 67
eyespyus
  • 1,576
  • 19
  • 20
  • You mean that all the code I wrote should be in the FriendPicker activity? Cause if it is so, there is something I don't get at all in the startActivityForResult process. – thomaus Mar 14 '11 at 17:48
  • I tried putting the onActivityResult function in my FriendPicker class and lo log either. I don't get it... – thomaus Mar 14 '11 at 18:18
  • 3
    onActivityResult() should be placed in the Activity where you called startActivityForResult() – Kevin Gaudin Mar 14 '11 at 19:37
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]

Ben Clayton
  • 80,996
  • 26
  • 120
  • 129
insomniac
  • 379
  • 5
  • 7
  • 2
    Actually it will work with any requestCode >= 0. Anything negative however doesn't. – whitebrow Apr 08 '13 at 15:43
  • THANKS!! by mistake I was sending a -1 and the instruction works (I meen it opens the Activity) but then it does not receive data, so you have to add a code >= 0 – Stornu2 Jun 03 '15 at 07:06
3

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?

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

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

msimons
  • 495
  • 10
  • 19
0

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.

Shachi
  • 1,858
  • 3
  • 23
  • 41
0

In case you're wondering about this in react-native in a native module:

in your MainActivity:

...
private ReactInstanceManager mReactInstanceManager;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    mReactInstanceManager.onActivityResult(requestCode, resultCode, data);
}

in your Module:

public class MyModule extends ReactContextBaseJavaModule implements ActivityEventListener {
    static final int REQUEST_VIDEO_CAPTURE = 1;

    final ReactApplicationContext reactContext;
    Promise promise;

    public GeneralIntentModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        this.reactContext.addActivityEventListener(this);
    }

    @Override
    public String getName() {
        return "MyModule ";
    }

    @ReactMethod
    public void captureVideo(Promise promise) {
        this.promise = promise;
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (takeVideoIntent.resolveActivity(this.reactContext.getPackageManager()) != null) {
            this.reactContext.startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE, null);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.promise.resolve(data.getDataString());
    }
}
f4z3k4s
  • 966
  • 7
  • 13