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;


 ? Default constructor for Fragment in Android
 ? How to use the twitter object in multiple fragments?
 ? How to Resolve This Fragment "non-default constructor" Error?
 ? Non-default constructors in fragments with DatePicker Callback
 ? Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]
 ? android release error Avoid non_default construtor in fragments
 ? Default constructors in fragments
 ? Fragments with empty constructors vs. newInstance
 ? Best practice for updating arguments of a fragment?
 ? Best practice for updating arguments of a fragment?