简体   繁体   English

超级OnActivityResult不调用Fragment OnActivityResult

[英]Super OnActivityResult not calling Fragment OnActivityResult

I'm using Xamarin Studio for my app. 我正在为我的应用程序使用Xamarin Studio。 I'm having an issue similar to this one here . 这里有一个与此类似的问题。 I'm calling StartActivityForResult with an intent that launches the camera. 我正在以启动相机的意图调用StartActivityForResult The camera takes the picture and returns to the application just fine, and the main activity's OnActivityResult is called. 相机拍照并返回到应用程序,然后调用主活动的OnActivityResult My issue is that, despite a call to base.OnActivityResult , my fragment's OnActivityResult is never hit. 我的问题是,尽管调用了base.OnActivityResult ,但我的片段的OnActivityResult从未被命中。

I've looked through the solutions here on SO, which are generally: 我在这里浏览了SO上的解决方案,这些解决方案通常是:

1. Call base.StartActivityForResult instead of StartActivityForResult inside the fragment 1.在片段内调用base.StartActivityForResult而不是StartActivityForResult

2. Call base.OnActivityResult in the main activity's OnActivityResult to forward on unhandled request codes 2.呼叫base.OnActivityResult在主要活动的OnActivityResult对未处理请求代码转发

3. Ensure launchMode in the manifest is not set to single 3.确保launchMode中的launchMode未设置为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 在SupportFragment中,您应该使用

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 注意arg resultCode的类型,以int形式覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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