Here I will explain the communication between fragments in
Android. The following is a brief description of the task that I am going to show you.
I have an Activity with two fragments, lets name that fragments to FragmentA and Fragment B. FragmentA contains a Button and FragmentB contains a textview.
Each time the user click on the Button of FragmentA then the textview in the FragmentB will updates its text like "this button is clicked <counter> times".
This is the task I am going to show you.
For this first create FragmentA and its Layout,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@android:color/darker_gray" android:layout_height="match_parent">
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:layout_margin="20dp" android:id="@+id/button" android:layout_gravity="center_horizontal" />
- </LinearLayout>
The FragmentA contains a Button and in the java code please load the xml file in onCreateView();
Now create FragmentB and its Layout,
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@android:color/white" android:layout_height="match_parent">
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/textView" android:layout_gravity="center_horizontal" />
- </LinearLayout>
FragmentB contains a Textview only. load this layout to FragmetB class on the onCreateView()
Now create the main part of the task that is communicator Interface.
- public interface Communicator
- {
- public void clicked(String text);
- }
Now create the MainActivity that implement Communicator Interface,
- public class MainActivity extends Activity implements Communicator
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public void clicked(String text)
- {
- FragmentB b = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2);
- b.chageText(text);
- }
- }
The clicked method will call another method named changeText(String) in Fragment B. Now we need to notify the communicator Interface when Button in FragmentA is clicked and create a method chageText(String) in FragmentB.
Now create Button Click Event in FragmentA. This is how the FragmentA will looks like,
- public class FragmentA extends Fragment
- {
- private int counter = 0;
- private Communicator comm = null;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- return inflater.inflate(R.layout.fragment_a, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState)
- {
- super.onActivityCreated(savedInstanceState);
- Button button = (Button) getActivity().findViewById(R.id.button);
- comm = (Communicator) getActivity();
- button.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- counter++;
- comm.clicked("This button is clicked " + counter + " times");
- }
- });
- }
- }
On the onActivityCreated() we are initialising the communicator with the FragmentA, and in the button click we are firing the interface method()
Now Create the FragmentB,
- public class FragmentB extends Fragment
- {
- private TextView textView = null;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
- {
- return inflater.inflate(R.layout.fragment_b, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState)
- {
- super.onActivityCreated(savedInstanceState);
- textView = (TextView) getActivity().findViewById(R.id.textView);
- }
- public void chageText(String data)
- {
- textView.setText(data);
- }
- }
There will be a method named chageText to change the content of textView in FragmentB. This is the way in which the fragments communicate in Android.
The out put will looks as follows,
Read more articles on Android: