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

风格不统一 提交于 2019-12-04 22:45:07

问题


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);

回答1:


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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/26637213/while-passing-android-dialogfragment-arguments-oncreatedialog-bundle-agument-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!