Passing data from one fragment to another

Hi,

I cant find any resources in how to pass a value/object from one fragment to another?

All my fragments are running in the same activity.

Can anybody help?

Posts

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    A couple of different ways. The easiest is to expose properties on one fragment, then set those properties. For example

    // Inside Fragment A which wants to provide data to fragment B.
    var fragmentB = (FragmentB) FragmentManager.FindFragmentByTag("fragmentB");
    fragmentB.FirstName = "Don";
    fragmentB.LastName="Quixote";
    

    You could also use events (i.e. Fragment A raises an event that Fragment B is listening for).

  • CarelMeyerCarelMeyer ZAMember ✭✭

    Thanks,

    Really did not expect it to be as simple as that!

  • DanHermesDanHermes USInsider, University ✭✭
    edited December 2015

    There's more to it since Fragments might contain editable data which can be destroyed when the device is rotated or when Android decides to go on a killing spree during low memory. Here are some guidelines:

    • Fragment constructor must always be empty (as it is called by Android on OS fragment restart)
    • Google encourages the use of bundles in all cases, however:
    • For read-only data, public properties and methods to set properties will probably work, though are non-standard.
    • For writable data, bundles are necessary to prevent data loss on device rotation and Android activity kills during low memory.
    • To restore data implement onSaveInstanceState() and/or onRestoreInstanceState() using a bundle.
  • GaryParkinGaryParkin USMember ✭✭

    I may be late but this is how I do it..
    (I'm using Android.Support.V4.App.Fragment, and Android.Support.V4.App.FragmentManager)

    In the Main Activity:

    Bundle utilBundle= new Bundle();
    utilBundle.PutString("SomeTag", "Some Data From Main");
    // Load the fragment
    var trans = SupportFragmentManager.BeginTransaction();
    // Add the utility screen
    trans.Add(Resource.Id.fragmentContainer, mFragUtility, this.Resources.GetString(Resource.String.action_utility));
    trans.Commit();
    // Add the parameter
    mFragUtility.Arguments = utilBundle;
    

    Then in my Fragment, in the OnCreateView

    // Display value
    string stringData = Arguments.GetString("SomeTag", string.Empty);
    
  • GaryParkinGaryParkin USMember ✭✭

    After testing this, I found it might be easier to set a SharedPreferences value, then get it in the fragment. That way if Android kills it, or the user gets a phone call and minimizes your app, you always have it available.

  • VijayPawar.0297VijayPawar.0297 USMember ✭✭
    edited December 2016

    @GaryParkin said:
    I may be late but this is how I do it..
    (I'm using Android.Support.V4.App.Fragment, and Android.Support.V4.App.FragmentManager)

    In the Main Activity:

    Bundle utilBundle= new Bundle();
    utilBundle.PutString("SomeTag", "Some Data From Main");
    // Load the fragment
    var trans = SupportFragmentManager.BeginTransaction();
    // Add the utility screen
    trans.Add(Resource.Id.fragmentContainer, mFragUtility, this.Resources.GetString(Resource.String.action_utility));
    trans.Commit();
    // Add the parameter
    mFragUtility.Arguments = utilBundle;
            
    

    Then in my Fragment, in the OnCreateView

    // Display value
    string stringData = Arguments.GetString("SomeTag", string.Empty);
    

    Thank you So much it helped me. For what I was looking on google that found here after 10 days.

  • GaryParkinGaryParkin USMember ✭✭

    Glad to help.

  • GaryParkinGaryParkin USMember ✭✭

    This is funny because I'm working on my second Android app. It's been 2 years since I learned this and I was looking for an answer to this question and answered myself 2 years ago. :D

    The correct way to construct the code for the Fragment is like this:

        // Display value
         if (Arguments != null)
         {
            string stringData = Arguments.GetString("SomeTag") ?? "Data not available";
         }
    

    Because sometimes the Bundle doesn't exist.

Sign In or Register to comment.