Looking at the documentation of DialogFragment
, one sees the static newInstance
method to initialize a new alert dialog fragment. My question is, why not use a constructor to do so, like this:
public MyAlertDialogFragment(int title) {
Bundle args = new Bundle();
args.putInt("title", title);
setArguments(args);
}
Isn't this exactly the same or does it differ somehow? What's the best approach and why?
If you overload the constructor with MyAlertDialogFragment(int title)
, the Android system may still call the default MyAlertDialogFragment()
constructor if the Fragment
needs to be recreated and the parameter is then not passed.
If you create a DialogFragment that receives objects through the constructor, you will have problems when android recreates your fragment. This is what will happen:
- your code creates the dialog calling the constructor you have created and passing some arguments as dependencies.
- your dialog runs, and uses the dependencies that you passed though the constructor
- the user closes the app
- time passes, and android kills the fragment to free memory
- the user opens the app again
- android will recreate your dialog, this time using the default constructor. No arguments will be passed!
- Your dialog will be in a undesired state. It may try to use instance variables that you expected to pass through the constructor, but as they are not there you'll get a null pointer exception.
To avoid this, you need not to rely on the constructor to establish the dependencies, but in in Bundles (arguments and saved instances). That may force you to implement Parcelable in some classes, which sucks.
EDIT: you can reproduce Android killing the app (step 4) by enabling the "don't maintain Activities" in the Development settings. That's the way to easily test it.
Android relies on Fragments having a public, zero-argument constructor so that it can recreate it at various times (e.g. configuration changes, restoring the app state after being previously killed by Android, etc.).
If you do not have such a constructor (e.g. the one in the question), you will see this error when it tries to instantiate one:
Fragment$InstantiationException: Unable to instantiate fragment
make sure class name exists, is public, and has an empty constructor that is public
Arguments given to it by Fragment.setArguments(Bundle)
will be saved for you and given to any new instances that are (re)created. Using a static method to create the Fragment simply provides an easy way to setup the required arguments whilst maintaining a zero-argument constructor.
Because when android is recreating a fragment, it always uses the empty constructor, and by using newInstance() you can set data that fragment uses when recreating, for example when the screen is rotated
for example:
public static FragmentExample newInstance(Parcelable uri) {
FragmentExample fragmentExample = new FragmentExample();
Bundle bundle = new Bundle();
bundle.putParcelable("Uri", uri);
fragmentExample.setArguments(bundle);
return fragmentExample;
}
来源:https://stackoverflow.com/questions/14011808/why-use-newinstance-for-dialogfragment-instead-of-the-constructor