Android Interface Class for Fragment Communication

Reading Time: 4 minutes

Android Interface class is the best practice for fragment communication. If we want to make two fragments to communicate, we must put some effort to avoid coupling. If one fragment have reference to another, and call it directly, this situation is called coupling, and called fragment is not reusable. More over, when the mobile device is rotated, application will terminate. So we need to arrange communication structure android fragment communication with activity as follows:


We will continue from previous example. In previous example we had one activity and “Upper” and “Lower” fragment. In upper fragment there is a “Click Me” button, and in “Lower” fragment there is text view for displaying number of “clicks”.
Then we need to map button in “Upper” from its XML to java code. In “Upper” fragment we would like to have an method that will react on button clicks. This method has to be related to certain event, and that event is onActivityCreated.

public void onActivityCreated(Bundle savedInstanceState){

So inside of onActivityCreated we map the button by using getActivity() class


In the same fashion, we should map text view in “Lower” fragment.


In order to add method for working with the button in fragment “Upper”, we need to expand initial fragment class to implement onClickListener from View class.

public class Upper extends Fragment {
public class Upper extends Fragment implements View.OnClickListener {


Then choose onClick(v:View):void.


Code is now as following:


Now, whenever user click on the button in “Upper” fragment, code in onClick will be executed. We can simply add one counting variable (count) and increment it whenever user click on the button.


In order to change text in fragment “Lower”, we need to declare one more method to change display.

public void changeDisplay(String some_data){



In order to bundle everything together we must create one interface class.


This class must be of an interface type.


Add in here one public method and call it for example response. Note that interface functions can’t have body:

public interface Comm {

public void response(String some_data);


We need now to override Main Activity method by implementing Comm interface class that we had just made.

public class MainActivity extends AppCompatActivity {
public class MainActivity extends Activity implements Comm {

Note that we first change  extends from AppCompatActivity to Activity and then implements Comm interface class.


Now, add one object of an Comm class in fragment “Upper”.


Then go to main activity class and make some code in the response method. Use FragmentManager class to produce an “Lower” fragment class object

public void response(String some_data) {

FragmentManager fragmentManager = getFragmentManager();

Lower lower =(Lower)fragmentManager.findFragmentById(;


Note: it is very important to find fragment by id from content_main.xml, NOT from fragment_lower.xml


We need to add onClickListener to the button in “Upper” fragment:


Once when lower object is created in “Lower” fragment, we need to call its method displayData.


And that’s it.

You can download fragment communicating with other fragments example

External links:
Android Interface class for fragment communication on YouTube
Android Interface class for fragment communication on simpledeveloper
Android Interface class for fragment communication on kb4dev
Android Interface class for fragment communication on techblogon


Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *