Fragment newInstance why use arguments?

In the first example, you've not actually set the fragment instance variables val1 and val2, so they remain uninitialized. Because of this, you'd be required to read back the bundle in onCreate to set the instance variables.

When the fragment instance is destroyed and re-created (e.g. due to a device rotation), the onCreate (or onCreateDialog for DialogFragments) can re-load the arguments using:

public void onCreate(Bundle savedInstanceState)
{
 if (savedInstanceState != null)
 {
  Bundle args = getArguments();
  val1 = args.getInt("val1");
  val2 = args.getInt("val2");
 }
}

and your state can be restored.


The arguments bundle is retained along with the onSaveInstanceState(Bundle), while constructor parameters are not.

Similarly to what happens if you don't save out your fields to onSaveInstanceState() in an Activity. Think of the arguments bundle like an Intent, Intents are also retained across process death and configuration change.