0

I'm using Xamarin Studio for my app. I'm having an issue similar to this one here. I'm calling StartActivityForResult with an intent that launches the camera. The camera takes the picture and returns to the application just fine, and the main activity's OnActivityResult is called. My issue is that, despite a call to base.OnActivityResult, my fragment's OnActivityResult is never hit.

I've looked through the solutions here on SO, which are generally:

1. Call base.StartActivityForResult instead of StartActivityForResult inside the fragment

2. Call base.OnActivityResult in the main activity's OnActivityResult to forward on unhandled request codes

3. Ensure launchMode in the manifest is not set to single

Code:

Fragment -

void AddImage(object sender, EventArgs e){

    //Preparing intent here...

    StartActivityForResult (intent, 0);
}

//no override keyword because I'm given a compiler error stating
//that there is no OnActivityResult to override
protected void OnActivityResult(int requestCode, Result resultCode, Intent data){
    //I call resultCode.GetHashCode here because I'm using SupportFragment
    base.OnActivityResult (requestCode, resultCode.GetHashCode(), data);

    if (resultCode == Result.Canceled)
        return;

    //process image...
}

Main Activity -

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult (requestCode, resultCode, data);
    //processing data from other intents at the main activity level
}
Community
  • 1
  • 1
jc_programmer
  • 27
  • 1
  • 7

1 Answers1

0

within a SupportFragment, you should use

public override void OnActivityResult(int requestCode, int resultCode, Intent data){
    base.OnActivityResult (requestCode, resultCode, data);

instead of

public override void OnActivityResult(int requestCode, Result resultCode, Intent data) {

note the type of the arg resultCode, as a int, it'll be available to override

Benoit Jadinon
  • 1,082
  • 11
  • 21
  • This worked perfectly! Thanks! I overlooked my use of `protected`, turns out it was giving me an error that I had misinterpreted as inappropriate use of `override`. Changing my method from `protected` to `public` and passing the proper type for `resultCode` fixed everything. – jc_programmer Aug 07 '15 at 19:57
  • you probably also copy-pasted the `OnActivityResult` from your Activity, right ? – Benoit Jadinon Aug 07 '15 at 20:07
  • I don't think I did -- I'd completely forgotten I ran `StartActivityForResult` in the main activity – jc_programmer Aug 10 '15 at 13:41