How to avoid non-default constructors in fragments?

This question already has an answer here:


You have to call fragment something like this:

    int id;

    Fragment newFragment = CountingFragment.newInstance(id);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(, newFragment);

    public static class CountingFragment extends Fragment {
    int mNum;

    static CountingFragment newInstance(int num) {
        CountingFragment f = new CountingFragment();
        Bundle args = new Bundle();
        args.putInt("num", num);

        return f;
    public void onCreate(Bundle savedInstanceState) {
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment, container, false);
        View tv = v.findViewById(;
        ((TextView)tv).setText("Hello. This is fragment example #" + mNum);
        return v;


 ? Do fragments really need an empty constructor?
 ? Passing an Object from an Activity to a Fragment
 ? Fragment.newInstance() vc onSaveInstanceState()
 ? Callback When DialogFragment is Dismissed
 ? How to replace a Fragment on button click of that fragment?
 ? Cannot create a default constructor from a class that extends DialogFragment and I have created my own custom constructor
 ? Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]
 ? Parameterised constructor for a fragment in android
 ? How to avoid non-default constructors in fragments?
 ? Default constructor for Fragment in Android