While passing Android DialogFragment arguments, onCreateDialog bundle agument is unexpectedly null
Asked Answered
P

2

8

I am trying to display a basic dialog in Android using a DialogFragment using an argument for the dialog message as described in StackOverflow thread and DialogFragment documentation. My problem is that the Bundle argument savedInstanceState in onCreateDialog always shows up as null, which means the activity displays an empty dialog box instead of one with a message. How can I get the non-null bundle contents from the newInstance factory method to show up in onCreateDialog? Or am I simply missing something else?

The only significant difference I see from the documentation is that I am using a non-static class. I want the positive dialog button to depend on the content of the message, so this is intentional.

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class SampleDialog extends DialogFragment {
    public static final String DIALOG_MESSAGE = "dialogMessage";

    private String dialogMessage;

    // arguments are handled through factory method with bundles for lifecycle maintenance
    public SampleDialog(){
    }

    public static SampleDialog newInstance(String dialogMessage){
        SampleDialog fragment = new SampleDialog();
        Bundle args = new Bundle();
        args.putString(DIALOG_MESSAGE, dialogMessage);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if(savedInstanceState != null) {
            dialogMessage = savedInstanceState.getString(DIALOG_MESSAGE);
        }

        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(dialogMessage)
                .setPositiveButton(R.string.dial, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // will depend on content of dialogMessage
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

I am calling this from an activity this way:

SampleDialog myDialog = SampleDialog.newInstance("does not appear");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
myDialog.show(transaction, TAG);
Plank answered 29/10, 2014 at 17:42 Comment(0)
M
19

you have to use getArguments to retrieve the Bundle you set with setArguments

Instead of

if(savedInstanceState != null) {
     dialogMessage = savedInstanceState.getString(DIALOG_MESSAGE);
}

you should have something like:

Bundle bundle = getArguments();
if (bundle != null) {
    dialogMessage = bundle.getString(DIALOG_MESSAGE);
}
Madra answered 29/10, 2014 at 17:45 Comment(0)
L
1

I had the same problem, you have to use getArguments, as Blackbelt said.

The savedInstanceState will be available when onSaveInstanceState (documentation) is called and some data is filled in outState, to be retrieved latter.

Labour answered 23/7, 2015 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.