Android DialogFragment newInstance optional parameters

1.3k views Asked by At

I've been following the official android developer docu and this one for the use of DialogFragment inside my project. so far so good, since I have to pass data to the DialogFragment for it to create a multichoice list, I call the DialogFragment from inside my MainActivity via the newInstance method (passing the items) and I am getting a correct result. Now, I would like to pass another argument, also data for the DialogFragment but it has to be optional since I dont need to pass it all the time. Is there a way for me to achieve this?

EDIT:
so I took the Advice from the comments below and created a setter and passed the items i wished to pass to the DiagramFragment. It worked just fine, sadly it didn't help me solve my problem. The reason I wanted to pass the second data is that I thought, if the user opens the DialogFragment and makes a selection and after that reopens the DialogFragment his last choice is gone. I wanted to check the checkboxes he already had checked programmatically, by passing the checked once back to the DialogFragment and then setting the right indexes back into mSelectedItems - but even tho the indexes are set correctly the checkboxes stay unchecked.. is there a workaround?

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

...

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checked the item, add it to the selected items
                       mSelectedItems.add(which);
                   } else if (mSelectedItems.contains(which)) {
                       // Else, if the item is already in the array, remove it
                       mSelectedItems.remove(Integer.valueOf(which));
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // or return them to the component that opened the dialog
                   ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ...
               }
           });

    return builder.create();
}
2

There are 2 answers

0
Blundell On

an optional parameter can be done like this:

static MyDialogFragment newInstance() {
        return newInstance(-1);
}

static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
}

You can call newInstance() if you have no number or newInstance(300) if you have.

On the other side:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     int num = getArguments().getInt("num");
     if(num == -1) {
       // no extra number
     } else {
       // do something with the extra number
     }

...

Alternatively instead of using -1 you could not add the Int at all and just check for the default value (I think its 0 in the API docs)

0
b101 On

I got it, it was already answered - funny I didn't find it when I googled..

There are easiest ways of doing what you want but let's take this approach: As you have noticed, the setMultiChoiceItems() method receives a String[] as the items of your DialogFragment and a boolean [] to define if they are checked or not. We are going to use this array to persist our choices. This array must have the same length as the items array and will be initially set to false.

So all I had to do was create a boolean[] itemsChecked and set the correct indexes to true, to re-check the right checkboxes..

Original Question Persist AlertDialog checked options - all credit to @joao2fast4u