传递Android DialogFragment参数时,onCreateDialog捆绑包参数意外为null [英] While passing Android DialogFragment arguments, onCreateDialog bundle agument is unexpectedly null
问题描述
我正在尝试使用DialogFragment在Android上显示基本对话框,并使用 StackOverflow线程和 DialogFragment文档。
我的问题是onCreateDialog中的Bundle参数saveInstanceState总是显示为null,这意味着活动显示一个空对话框,而不是一个带有消息的对话框。如何从newInstance工厂方法获取非空捆绑包内容以显示在onCreateDialog中?还是我只是想念其他东西?
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();
}
}
我是通过以下方式在活动中称呼它的: / p>
I am calling this from an activity this way:
SampleDialog myDialog = SampleDialog.newInstance("does not appear");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
myDialog.show(transaction, TAG);
推荐答案
您必须使用 getArguments
检索使用 setArguments
而不是
if(savedInstanceState != null) {
dialogMessage = savedInstanceState.getString(DIALOG_MESSAGE);
}
您应该有类似以下内容:
you should have something like:
Bundle bundle = getArguments();
if (bundle != null) {
dialogMessage = bundle.getString(DIALOG_MESSAGE);
}
这篇关于传递Android DialogFragment参数时,onCreateDialog捆绑包参数意外为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!