Android Fragment Communication Through Activity

This is a demonstration of communications between fragments through an Activity. The Activity acts like a tunnel which connects the fragments. You have two Fragments in an Activity, and Fragment 1 wants to talk to Fragment 2. In this example, Fragment 1 has two Buttons, Fragment 2 has a TextView to display a counter. When a button in Fragment 1 is clicked, the TextView in Fragment 2 has to increase by one or decrease by 1.

A solution to this is to use an Interface class. Make the Activity to implement this interface, Fragment 1 will call the interface method that will be implemented by the Activity class, this method in the Activity class will route the data from Fragment 1 to Fragment 2.

1. The interface Communicator.java

public interface Communicator {
	public void respond(String data);
}

2. The layout for MainActivity.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <fragment
        android:id="@+id/frag1"
        android:name="com.example.fragmentintercomm.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <fragment
        android:id="@+id/frag2"
        android:name="com.example.fragmentintercomm.FragmentTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/frag1"/>
</RelativeLayout>

3. The MainActivity.java class

public class MainActivity extends Activity implements Communicator {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public void respond(String data) {
		FragmentManager fm = getFragmentManager();
		FragmentTwo f2 = (FragmentTwo) fm.findFragmentById(R.id.frag2);
		f2.updateDisplay(data);
	}
}

4. The FragmentOne.java class with 2 buttons, increment and decrement. When one of the button is clicked, it should update the counter display in in Fragment 2.

public class FragmentOne extends Fragment  {
	private Button incrementBtn;
	private Button decrementBtn;
	private int counter = 0;

	Communicator com; // communication interface object

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			com = (Communicator) activity; //Reference to Main Activity
		} catch (ClassCastException castException) {
			Log.e("FragmentOne", "MainActivity didn't implement the Communicator interface");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_one, container,false);
		incrementBtn = (Button) v.findViewById(R.id.btn1);
		decrementBtn = (Button) v.findViewById(R.id.btn2);

		incrementBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				counter++;
				if (com != null) com.respond("Counter: " + counter); //Call the method in the activity class
			}
		});

		decrementBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				counter--;
				if (com != null) com.respond("Counter: " + counter); //Call the method in the activity class
			}
		});

		return v;
	}
}

5. The FragmentTwo.java class

public class FragmentTwo extends Fragment {
	TextView tvDisplay;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_two, container,false);
		tvDisplay = (TextView) v.findViewById(R.id.tv_display);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	public void updateDisplay(String data){
		tvDisplay.setText(data);
	}
}

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search