How to avoid non-default constructors in fragments?

This question already has an answer here:


ANSWERS:


You have to call fragment something like this:

    int id;

    Fragment newFragment = CountingFragment.newInstance(id);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.simple_fragment, newFragment);
    ft.addToBackStack(null);
    ft.commit();

    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);
        f.setArguments(args);

        return f;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Hello. This is fragment example #" + mNum);
             tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
        return v;
    }
}


 MORE:


 ? 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