Xamarin Android从Activity传递变量到Fragment返回null

时间:2014-10-22 13:50:58

标签: android android-activity android-fragments xamarin

我正在尝试将变量从我的主要活动传递到片段。这就是我试图这样做的方式:

这是我的活动:

Bundle args = new Bundle (); 
    args.PutString ("header", header);
    args.PutString ("content", content);
    args.PutString ("footer", header);
    args.PutString ("imageLocation", imageLocation);

    exhibitMainFragment.Arguments = args;

    FragmentManager.BeginTransaction ()
        .Replace (Resource.Id.main_view, exhibitMainFragment)
        .AddToBackStack (null)
        .Commit ();

这是我的片段:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
        {

            var ignored = base.OnCreateView(inflater, container, savedInstanceState);

            var view = inflater.Inflate(Resource.Layout.MuseumInformation, null);

            content = this.Activity.Intent.GetStringExtra ("content");
            header = this.Activity.Intent.GetStringExtra ("header");
            footer = this.Activity.Intent.GetStringExtra ("footer");
            imageFilePath = this.Activity.Intent.GetStringExtra ("imageLocation");

但是没有传递任何变量(它们在片段中都是空的)。我在这里显然犯了一个根本性的错误。有人能告诉我它是什么吗?或者向我展示一种更好的方法来传递数据。

感谢。

2 个答案:

答案 0 :(得分:2)

我就这样做了:

content = Arguments.GetString("content"); 
header = Arguments.GetString ("header");
footer = Arguments.GetString("footer");
imageFilePath = Arguments.GetString ("imageLocation");

答案 1 :(得分:2)

在活动中

Bundle mybundle = new Bundle();
mybundle.PutString("MyDataTag", "Hello");

FragmentTransaction fragmentTransaction = FragmentManager.BeginTransaction();
var myFragment = new VerifyReportFragment();
myFragment .Arguments = mybundle;
Fragment OnCreateView

中的

String stringData= Arguments.GetString("MyDataTag");