I have this piece of code:
public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener) {
DateDialogFragment dialog = new DateDialogFragment();
mContext = context;
mListener = listener;
/*I dont really see the purpose of the below*/
Bundle args = new Bundle();
args.putString("title", "Set Date");
dialog.setArguments(args);
return dialog;
}
Pretty self explanatory, but what I don’t understand is what the point of giving it a Bundle. I never really make use of it I guess. The Android Doc’s explanation of this (for Fragments) is here:
http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle)
What exactly does a construction argument mean? Since its never used, I fail to see the use of it. Any explanation much appreciated. Thanks.
It is simply a generic mechanism for you to attach data values that you might want to use to configure the
Fragmentor otherwise read during operation, similar to how you might pass extras in aBundleto a newActivityvia it’sIntent.I do agree, however, that since a
Fragmentcan be instantiated using its constructor, where anActivitycannot, the usefulness of the API may seem compromised because you can just as easily configure aFragmentusing setter methods and member variables inside ofnewInstance()before returning the instance. For example, your code could implement a method on theFragmentcalledsetTitle()that you could call, and you wouldn’t need to pass that as an argument. However, arguments do provide a nice way of storing this information as key/value data if that model fits your application.One key distinction about the arguments of a
Fragmentis that they are retained as part of the saved instance state. So if your UI rotates or some other change requires theFragmentto be recreated, the argumentsBundleattached will be retained and handed back to the new instance.HTH