简体   繁体   中英

Super OnActivityResult not calling Fragment OnActivityResult

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM