title: Android fragment factory method vs constructor overloading
body: <p>First of all, I already know that the FragmentManager often destroys then re-creates the Fragment using the default constructor. The coders must save important things in a Bundle of arguments once in the factory method, then take them out every time the Fragment is re-created in <strong>onCreate(Bundle)</strong>.</p>
  
  <pre><code>public class MyFragment extends Fragment {
      private static final String MY_STRING_CONSTANT = "param";
      private int mIntegerMember;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mIntegerMember= getArguments().getInt(MY_STRING_CONSTANT);
      }
  }
  </code></pre>
  
  <p>My question is, is there any difference between this:</p>
  
  <pre><code>// Inside MyFragment.java
  public MyFragment() {
      // No-argument constructor required by the FragmentManager.
  }
  public static MyFragment newInstance(int param) {
      // Factory method
      MyFragment fragment = new MyFragment();
      Bundle args = new Bundle();
      args.putInt(MY_STRING_CONSTANT, param);
      fragment.setArguments(args);
      return fragment;
  }
  
  // Somewhere else
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  transaction.add(R.id.frame_layout, MyFragment.newInstance(123)).commit();
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>// Inside MyFragment.java
  public MyFragment() {
      // No-argument constructor required by the FragmentManager.
  }
  public MyFragment(int param) {
      // Parameterized constructor
      Bundle args = new Bundle();
      args.putInt(MY_STRING_CONSTANT, param);
      setArguments(args);
  }
  
  // Somewhere else
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  transaction.add(R.id.frame_layout, new MyFragment(123)).commit();
  </code></pre>
  
  <p>I can see nothing which prevents the FragmentManager from calling the no-argument constructor. And the data I save in the parameterized constructor (in a Bundle object) will be preserved and restore during <strong>onCreate()</strong>, just like when I use the factory method.</p>