Android - Communication between Fragments


In the last tutorial we learned about basics of fragments. In this note we will implement two fragments and then we will make them communicate with each other.

After incrementing

There can be many scenarios where communication between fragment is required. You need to pass data between fragments on button click event. You may also use android toolbar to switch between fragments. When you add buttons to your toolbar, you need to dynamically change screen usign fragment.

Create an interface which will help us to communicate

communicate.java

package com.example.kb4devfragments;

interface communicate {
  public void sendData();
}

Whichever class implements the communicate interface need to implement sendData() method.

Make our main activity implement the communicate interface

package com.example.kb4devfragments;

import android.os.Bundle;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements communicate{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void sendData() {
		// TODO Auto-generated method stub
	
	// From fragments we can call this method with the help of reference of communicate. 
	}

}

Implementing First Fragment

  • If you don't know about fragments then please read our previous tutorial about fragments.
  • Create a reference of button.
     Button button;
  • We know that MainActivity implements the communicate interface. So, MainActivty must implement the sendData() method. We can access the implemented sendData() method by creating a reference of communicate and then by assigning the reference of activity to it.
    communicate cm;
    cm = (communicate) getActivity();
  • In the onClick() call the sendData() to shift the control from fragment to activity.
    cm.sendData();

FragmentA.java

package com.example.kb4devfragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentA extends Fragment implements OnClickListener{
 Button button;
 communicate cm;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		return inflater.inflate(R.layout.layout_fragmenta, container, false);
	}

	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		cm = (communicate) getActivity();
		button = (Button) getActivity().findViewById(R.id.button1);
		button.setOnClickListener(this);
	}



	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		cm.sendData();
	}
	
}

Now communicating with FragmentB from MainActivity

Add the following code to sendData() method of the MainActivity

  • Get the reference of FragmentManager which will help us to access available fragments for the current activity.
  • Retrieve the fragment by finding it with the help of its id.
  • Call the increamentData() function of FragmentB.
FragmentManager fm = getSupportFragmentManager();
FragmentB frag = (FragmentB) fm.findFragmentById(R.id.fragmentB);
frag.incrementData();

Implement FragmentB

onActivityCreated() is the callback method which is called by the system whenever the activity is created. So we can get the reference of the view-items in this method.

  • Get the text from the text view.
  • Convert string to integer.
  • Increament the previous value.
  • Set the new value by converting integer to string and passing the value as an argument to setText() method.
package com.example.kb4devfragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentB extends Fragment{
int count;
TextView textView1;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		return inflater.inflate(R.layout.layout_fragmentb, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		textView1 = (TextView)getActivity().findViewById(R.id.textView1);
		
	}

	public void incrementData(){
		String string = textView1.getText().toString();
		count = Integer.parseInt(string);
		count++;
		textView1.setText(String.valueOf(count));
	}

}