0

My question is a design question. I have two custom fragments CustomFrag1 and CustomFrag2. Both these fragments have a method swapCursor.

CustomFrag1:

public class CustomFrag1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // .... code
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // .... code

        MyAdapter mAdapter = new MyAdapter(); 

        // .... code
    }

    public static void swapCursor(final Cursor cursor, Activity ctx){

        // .... code
    }

}

CustomFrag2:

public class CustomFrag1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // .... code
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // .... code

        MyAdapter mAdapter = new MyAdapter(); 

        // .... code
    }

    public static void swapCursor(final Cursor cursor, Activity ctx){

        // .... code
    }

}

I don't want to make a seperate adapter class for each fragment. I do however, want to call swapCursor in the adapter class. If the adapter has been instantied from CustomeFrag1 I want swapCurosr to swap the cursor in CustomFrag1. If the adapter has been instantiated from CustomeFrag2 I want swapCurosr to swap the cursor in CustomFrag2.

Is it possible to pass in an instance of the Fragment...

MyAdapter mAdapter = new MyAdapter(this);

...and somehow represent that instance with a generic variable that has swapCursor defined as a method? If not, what strategy can I use? Interfaces? Generics? Something?

edit

I tried inheriting swapCursor but that would just swap out the cursor out on the parent and not the child.

the_prole
  • 8,275
  • 16
  • 78
  • 163
  • *I tried inheriting swapCursor but that would just swap out the cursor out on the parent and not the child*, why ? – Blackbelt Jan 17 '17 at 08:51

2 Answers2

1

swapCursor shouldn't be static at the first place, because there's no inheritance for static methods.

You could create an interface (SwappableCursor as a name hint) which has a method swapCursor, then all of your fragments can implement this interface. So that you'll have to implement swapCursor.

In MyAdapter's constructor you can add a parameter which has the type of your interface (SwappableCursor). You'll be able to invoke swapCursor method.

pcjuzer
  • 2,724
  • 4
  • 25
  • 34
  • Thanks. That was the strategy I was trending towards. Why can't static methods be inherited? – the_prole Jan 17 '17 at 09:04
  • Here are some answers which probably help: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods The first answer is quite good. – pcjuzer Jan 17 '17 at 09:51
0

Use interfaces.

public interface SwapFrag { 
   public swapCursor(final Cursor cursor, Activity ctx);
}

your fragments need to implement SwapFrag:

public class CustomFrag1 extends Fragment implements SwapFrag

and also need to implements the swapCursor method.

@Override
public static void swapCursor(final Cursor cursor, Activity ctx) {
  ...
}
Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32