“onActivityResult()”似乎没有收到结果

问题描述 投票:1回答:2

我有两个活动,一个只有一个巨大的列表视图,另一个有一个plainText和按钮。

这个“newRoll”活动需要返回一个添加到ObservableCollection的字符串,我没有实现listView更新但使用logcat的部分,我可以告诉没有任何东西被添加到包含所有项目的ObservableCollection中。有什么我误解了意图是如何工作的,从我得到的应该是什么工作?

主要活动:

    //upon clicking any item in the listView this runs:

    Intent NewRoll = new Intent(this, typeof(NewRoll));
    StartActivityForResult(NewRoll, requestCode);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            //just holds the string
                    holder = data.GetStringExtra("roll_name");
            //adds it to an observable collection
                    rolls.Add(holder);
            //me trying to get something back
                    Log.Info(rolls.Count.ToString(), "Roll Added");
                    Log.Info(holder, "Please?");
        }

NewRoll:

//on button click
//get whatever is in the plaintext
                newRoll = rollName.Text;
                data.PutExtra("roll_name",newRoll);
                SetResult(Result.Ok, data);
                Finish();
c# android visual-studio xamarin xamarin.android
2个回答
0
投票

Xamarin上的文档指出Android.App.Activity.OnActivityResult的签名是

[Android.Runtime.Register("onActivityResult", "(IILandroid/content/Intent;)V", "GetOnActivityResult_IILandroid_content_Intent_Handler")]
protected virtual Void OnActivityResult (Int32 requestCode, [Android.Runtime.GeneratedEnum] Result resultCode, Intent data)

所以你必须覆盖它以接收活动结果,如

protected override Void OnActivityResult (Int32 requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    // do your stuff
}

0
投票
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        //just holds the string
                holder = data.GetStringExtra("roll_name");
        //adds it to an observable collection
                rolls.Add(holder);
        //me trying to get something back
                Log.Info(rolls.Count.ToString(), "Roll Added");
                Log.Info(holder, "Please?");
    }

我在这里看不到@Override注释。你确定你压倒Activity基类方法onActivityResult

© www.soinside.com 2019 - 2024. All rights reserved.