0

I see that on google's example for creating a new fragment is through a static method called newInstance. But why aren't we using two constructors to achieve the same thing as below?

ie.

public class MyFragment extends Fragment {
    public MyFragment() {
    }

    public MyFragment(String name) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_NAME, name);
        setArguments(bundle);
    }
}

public class MyFragment extends Fragment {
    public MyFragment() {
    }

    public static MyFragment newInstance(String name) {
        MyFragment myFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_NAME, name);
        myFragment.setArguments(bundle);
        return myFragment;
    }
}
Rubin Yoo
  • 2,674
  • 2
  • 24
  • 32
  • 1
    This has been discussed on http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – Jon Goodwin Nov 25 '16 at 19:16
  • @JonGoodwin it's more related to the answer below. http://stackoverflow.com/a/25994861/1915831. Also, this isn't one constructor solution I'm trying solve. I do understand the impact of having one constructor and why it would be bad. – Rubin Yoo Nov 25 '16 at 20:20

0 Answers0