-2

I've been working between Fragment's using FragmentManager's to keep the view of the navigation drawer in my project.

What I want is to get a String from Fragment A and show it on a TextView on the Fragment B. As I'm not working with Intent's, I don't know how to do that. Can anyone please help me?

Here is a piece of my code to explain how i change the views in my project.

Button button = (Button) view.findViewById(R.id.btn_next);
button.setOnClickListener(new View.OnClicklistener(){

    @Override
    public void onClick(View v){

        Fragment frag_A = new ViewClassB();
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction()
            .replace(R.id.container, frag_A)
            .commit();

    }                           
});

I have the Main class that have a xml with the Fragment Views called container, there is where i show the content of the Activity.

Here is an example for that XML:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ViewClassA"
    >


    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#fff"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Welcome"
            android:id="@+id/text1"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:textColor="#52D5EC" />

         </LinearLayout>
     </FrameLayout>
 </android.support.v4.widget.DrawerLayout>

That's how I show the content in my project. In the View class B I just do a onCreateView to inflate the elements and work with them.

Again, what I need is to pass a String from the Fragment A to the Fragment B. I'm not using intents.

Can anyone help me, please?

galath
  • 5,717
  • 10
  • 29
  • 41
  • you can use fragment.putArguments – Ali Helmy Jul 10 '15 at 20:44
  • possible duplicate of [How to pass a value from one Fragment to another in Android?](http://stackoverflow.com/questions/23250707/how-to-pass-a-value-from-one-fragment-to-another-in-android) – Anand Singh Jul 10 '15 at 20:46

2 Answers2

0

From the Fragment documentation:

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

You can use Bundle to pass data from one Fragment to other:

MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putString(<KEY>, <VALUE>);
myFragment.setArguments(args);

And at the receiver's end:

String value = getArguments().getString(<KEY>, <DEFAULT_VALUE>);
Green goblin
  • 9,898
  • 13
  • 71
  • 100
0

The way to share content between fragments depends on your preferences. There are many options, so choose the one you feel more comfortable with:

  • Store the string in the activity that contains the fragments. The activity will act as a callback for the first fragment so it can receive this string when it changes. Since the activity is the one that swaps between them, just put the value in a bundle to provide the second fragment with the string value of the first one. (For me this is not the best option, but up to you)
  • Store the string in the first fragment and provide a getter so the activity can get it. When there is a need to swap between fragments, just get the value on the first fragment and provide it in a bundle to the second one.
  • If both fragments live together (which looks like it is not the case on your app) the clean option is to create an interface, implemented by the fragment that consumes the String, and used by the fragment which provides the String. Just let one fragment to know the other by this interface (not as the fragment itself).

Now, some code on this. To put a bundle on a fragment:

 Bundle bundle = new Bundle();
 bundle.putString("someString", stringValue);
 fragment.putArguments(bundle);

To get this bundle value inside the fragment: fragment.getArguments().getString("someString");

To get the current fragment that is in your container use the method FragmentManager#findFragmentById or FragmentManager#findFragmentByTag.

droidpl
  • 5,872
  • 4
  • 35
  • 47