J. Joe
J. Joe

Reputation: 381

Passing string[] between fragment. How to do that?

fragment1:

lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {

				var intent = new Intent (this, typeof(TracksByGenres));
				intent.PutStringArrayListExtra ("keys",	items);
				StartActivity (intent);
			};
		

TracksByGenres is fragment2

Error:The best overloaded method match for `Android.Content.Intent.Intent(Android.Content.Context, System.Type)' has some invalid arguments atnew Intent (this, typeof(TracksByGenres));

Fragment2:

public async override void OnActivityCreated(Bundle savedInstancesState)
		{
			base.OnActivityCreated (savedInstancesState);
			paramKey = Intent.Extras.GetStringArray ("keys").ToString();

			lst = View.FindViewById<ListView> (Resource.Id.lstHome);

			

Where is incorrect?

Upvotes: 0

Views: 197

Answers (1)

Sreeraj
Sreeraj

Reputation: 2444

var intent = new Intent (this, typeof(TracksByGenres));
            intent.PutStringArrayListExtra ("keys", items);
            StartActivity (intent);

Above snippet is to create an instance of a new Activity and launch it. Second parameter , typeof(TracksByGenres) should be actually typeof(A class inheriting from Actiivty (not fragment)) This is causing the exception.

Inorder to switch Fragments, you need to use FragmentTransaction . Here is a tutorial about how to manage fragments.

EDIT : Example of passing Data to Fragment , asked in comment

public class TracksByGenres :Fragment
{
  string mData;
  public void AddData(string data)
  {
     mData=data;
  }
  public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
      // inflate layout and store in variable
       //you can use data here
       // myTextView.Text=mData;
   }
}

Then In Activity,

Creating First Fragment, adding Data and Loading in FrameLayout

FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
TracksByGenres fragment = new TracksByGenres();

fragment.AddData("Xamarin is awesome");
// The fragment will have the ID of Resource.Id.fragment_container.
fragmentTx.Add(Resource.Id.fragment_container, fragment);

// Commit the transaction.
fragmentTx.Commit();

Replacing the fragment with a new Fragment, adding data. In this example Im using the same fragment for ease.

TracksByGenres aDifferentDetailsFrag = new TracksByGenres();

aDifferentDetailsFrag.AddData("Xamarin is awesome");

// Replace the fragment that is in the View fragment_container (if applicable).
fragmentTx.Replace(Resource.Id.fragment_container, aDifferentDetailsFrag);

// Add the transaction to the back stack.
fragmentTx.AddToBackStack(null);

// Commit the transaction. fragmentTx.Commit();

Upvotes: 0

Related Questions