超级OnActivityResult没有调用Fragment OnActivityResult

时间:2015-08-07 16:49:49

标签: android android-fragments xamarin

我正在为我的应用程序使用Xamarin Studio。我遇到了与this one here类似的问题。我打电话给StartActivityForResult,意图启动相机。相机拍摄照片并返回到应用程序就好了,并且调用了主要活动的OnActivityResult。我的问题是,尽管打电话给base.OnActivityResult,但我的片段OnActivityResult永远不会被击中。

我已经在SO上查看了解决方案,通常是:

1.在片段中调用base.StartActivityForResult而不是StartActivityForResult

2.致电主要活动base.OnActivityResult中的OnActivityResult以转发未处理的请求代码

3.确保清单中的launchMode未设置为single

代码:

片段 -

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

主要活动 -

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
}

1 个答案:

答案 0 :(得分:0)

在SupportFragment中,您应该使用

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

而不是

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

注意arg resultCode的类型,作为int,它可以覆盖