What is the point of setArgument in DialogFragment's?

android, bundle

Solution

It is simply a generic mechanism for you to attach data values that you might want to use to configure the `Fragment` or otherwise read during operation, similar to how you might pass extras in a `Bundle` to a new `Activity` via it's `Intent`.

I do agree, however, that since a `Fragment` can be instantiated using its constructor, where an `Activity` cannot, the usefulness of the API may seem compromised because you can just as easily configure a `Fragment` using setter methods and member variables inside of `newInstance()` before returning the instance. For example, your code could implement a method on the `Fragment` called `setTitle()` 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 `Fragment` is that they are retained as part of the saved instance state. So if your UI rotates or some other change requires the `Fragment` to be recreated, the arguments `Bundle` attached will be retained and handed back to the new instance.

HTH

Problem

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.

Original source