Android Interface Class for Fragment Communication

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:

Android-Interface-class-for-fragment-communication

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

upper-fragment-onActivityCreated-code1

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

lower-fragment-onActivityCreated-code1

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 {

upper-fragment-implements-View-onClickListener

Then choose onClick(v:View):void.

upper-fragment-implements-View-onClickListener-onclick

Code is now as following:

upper-fragment-implements-View-onClick

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.

upper-fragment-implements-View-onClick2

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

public void changeDisplay(String some_data){

text.setText(some_data);

}

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

new-java-class-for-interfacing

This class must be of an interface type.

new-java-interface-class-for-interfacing

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.

Main-Activity-implemented-Comm

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

Insert-comm-object-into-upper-fragment

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(R.id.fragment2);

}

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

Make-object-of-an-lower-class-in-main-activity

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

set-on-click-listener-on-button-in-upper-fragment

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

call-displayData-method

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

 

Posted in android Tagged with: ,

Leave a Reply

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

*