Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android passing variable from Activity to Fragment returns null

I'm trying to pass variables from my main activity to a fragment. This is how i'm attempting to do it:

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 Fragment:

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 none of the variables are being passed (they are all empty in the fragment). I'm clearly making a fundamental error here. Can somebody tell me what it is please. Or show me a better way to pass the data across.

Thanks.

like image 971
Can'tCodeWon'tCode Avatar asked Oct 22 '14 13:10

Can'tCodeWon'tCode


People also ask

Can getActivity return null?

getApplicationContext() error Android Assuming the fragment is attached to an activity so getActivity() will return a non-null object, which is usually true.

What are the methods can Override in fragment class?

The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.

When fragment is DESTROYED?

Fragment is destroyed. As with an Activity , you can save the variable assignments in a Fragment . Because data in a Fragment is usually relevant to the Activity that hosts it, your Activity code can use a callback to retrieve data from the Fragment , and then restore that data when recreating the Fragment .

What is life cycle of fragment in android?

Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. A fragment can implement a behaviour that has no user interface component.


2 Answers

This is how i did it:

content = Arguments.GetString("content"); 
header = Arguments.GetString ("header");
footer = Arguments.GetString("footer");
imageFilePath = Arguments.GetString ("imageLocation");
like image 100
Can'tCodeWon'tCode Avatar answered Oct 04 '22 20:10

Can'tCodeWon'tCode


In the Activity

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

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

in the Fragment OnCreateView

String stringData= Arguments.GetString("MyDataTag");
like image 20
Amalan Dhananjayan Avatar answered Oct 04 '22 22:10

Amalan Dhananjayan