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:


 ? 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?