3

Which is the correct way to make fragments inheritance with newInstance pattern?

For example, if we have a fragment MyFragment that inherits from another fragment SuperFragment which has the newInstance pattern --> https://stackoverflow.com/a/28855715/5125212

public class SuperFragment extends Fragment{
    public static SuperFragment newInstance(int var1, boolean var2){
        SuperFragment fragment = new SuperFragment();
        Bundle b = new Bundle();
        b.putInt("my_var1", var1);
        b.putBoolean("my_var2", var2);
        fragment.setArguements(b);
        return fragment
    }
// All other methods
}

This gets an error on "super":

public class MyFragment extends SuperFragment{
    public static MyFragment newInstance(int var1, boolean var2){
        return super.newInstance(int var1, var2);
    }
// All other methods
}

And this gets an error on constructor as we should avoid non-default constructors:

public class MyFragment extends SuperFragment{
    public MyFragment(int var1, boolean var2){
        newInstance(var1, var2);
    } 
// All other methods
}

And I found this to don't have any error but I don't like it because it semms to get recursively:

public class MyFragment extends SuperFragment{
    public static SuperFragment newInstance(int var1, boolean var2){
        return newInstance(var1,var2);
    }
}
Community
  • 1
  • 1
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65

2 Answers2

2

Find it!

Super fragment:

public class SuperFragment extends Fragment{
    public static Bundle setArgs(int var1, boolean var2){
        Bundle b = new Bundle();
        b.putInt("my_var1", var1);
        b.putBoolean("my_var2", var2);
        return b;
    }
// All methods you want in the super fragment, so they will be in the 
// inherit fragment too
}

Inherit fragment:

public class MyFragment extends SuperFragment{
    public static MyFragment newInstance(int var1, boolean var2){
        MyFragment fragment = new MyFragment();
        fragment.setArguments(setArgs(var1,var2));
        return fragment;
    }
// All other methods that you only want them on the inherit (this)     
// fragment
}
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
  • This wouldn't even compile because you're trying to return a SuperFragment instead of a MyFragment... – Kacy Aug 22 '17 at 01:31
0

You don't have to create the overloaded constructor. You pass your data to newInstanace(var1, var2) and then you do like this:

public static MyFragment newInstance(int var1, int var2)
{
  MyFragment fragment = new MyFragment(); // default constructor by android system
  Bundle b = new Bundle();
  b.putInt("my_var1", var1);
  b.putInt("my_var2", var2);

fragment.setArguements(b);

return fragment
}

and then in onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        int var1 = arguments.getInt("my_var1", 0);

UPDATE

Inheritance with newInstance pattern can't be done as static methods can't be overriden. Morever, if you dont have the default constructor, it will be a compile time error to overload default constructor

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46