Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 537
mattm Avatar asked Oct 29 '14 17:10

mattm


2 Answers

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);
}
like image 132
Blackbelt Avatar answered Sep 22 '22 11:09

Blackbelt


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.

like image 26
KuroObi Avatar answered Sep 19 '22 11:09

KuroObi