2

Can someone please tell me if I'm solving this correctly or if I should go another route?

This is a simplified example: I have 1 Activity and 2 Fragments. Each Fragment has a button that when clicked, relays the click back to the Activity and a Toast pops up within the Activity.

I know that a Fragment communicates with an Activity through an interface. But what If I have multiple Fragments that have a similar Interface. For example, here both Fragments use an onClick type of interface to communicate back to the Activity

 static interface OnClickedListener{
    public void buttonClicked(View v);
}

Is it better to

A) Create a separate Interface class and attach it within both Fragments. For example Fragment 1:

public class Fragment1 extends Fragment implements OnClickedListener{


private OnClickedListener clickedInterface;

public Fragment1() {
    // Required empty public constructor
}

@Override
public void buttonClicked(View v) {
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.clickedInterface = (OnClickedListener)activity;
}}

Fragment 2:

public class Fragment2 extends Fragment implements OnClickedListener{

private OnClickedListener clickedInterface;

public Fragment2() {
    // Required empty public constructor
}

@Override
public void buttonClicked(View v) {
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.clickedInterface = (OnClickedListener)activity;
}

OR

B) Create individual Interfaces unique to the specific Fragment and implement those in the MainActivity instead of the one Interface like mentioned above. Thank you.

Mark F
  • 1,523
  • 3
  • 23
  • 41

3 Answers3

5

First Create your custom fragment which is in implement interface.

    public class CustomFragment extends Fragment implements OnClickedListener{
        public OnClickedListener clickedInterface;

        @Override
        public void buttonClicked(View v) {
        }

        @Override
        public void onAttach(Activity activity) {
             super.onAttach(activity);
             this.clickedInterface = (OnClickedListener)activity;
        }
}

Now, you can add in every fragment

(i) Fragment 1

public class Fragment1 extends CustomFragment {
    ......
}

(ii) Fragment 2

public class Fragment2 extends CustomFragment {
    ......
}
Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54
0

I wouldn't let the details of any Views contained in a Fragment leak out into the Activity.

Better interfaces would be based on the semantic action involved with the button press, for example createThing() or deleteThing().

The Activity shouldn't really care which View was clicked to initiate the action just that the action needs to happen. When happens to your interfaces if you move the button to the menu, or somewhere else not associated with a View.

If you create interfaces like this, the question of creating copies for different Fragments disappears.

alex
  • 6,359
  • 1
  • 23
  • 21
0

There is a code principal called SOLID. "I" states for https://en.wikipedia.org/wiki/Interface_segregation_principle. It is a good practice to

You should not make universal interface or large ones, you need to create interface that can be "readable" and "understandable" for everyone by interface name and its method names like "articleSelected" or "loginProcessing(String loginName)" etc

aelimill
  • 1,015
  • 1
  • 10
  • 17