Xamarin variable for Android from Activity to Fragment returns null

I am trying to pass variables from my main activity to a fragment. Here is how I am trying to do this:

This is in my activity:

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 (); 

This is in my snippet:

 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"); 

But not one of the variables is passed (they are all empty in the fragment). I am clearly making a fundamental mistake here. Can someone tell me what it is. Or show me the best way to pass data through.

Thanks.

+5
source share
2 answers

Here is how I did it:

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

In action

 Bundle mybundle = new Bundle(); mybundle.PutString("MyDataTag", "Hello"); FragmentTransaction fragmentTransaction = FragmentManager.BeginTransaction(); var myFragment = new VerifyReportFragment(); myFragment .Arguments = mybundle; 

in fragment OnCreateView

 String stringData= Arguments.GetString("MyDataTag"); 
+2
source

Source: https://habr.com/ru/post/1205274/


All Articles