Android 屏幕旋转后DialogFragment按钮不工作

Android 屏幕旋转后DialogFragment按钮不工作,android,android-fragments,android-dialogfragment,Android,Android Fragments,Android Dialogfragment,在代码中,我创建了一个对话框片段,在搜索了不同的博客后,我能够在屏幕旋转后保留该对话框片段,但当我将其用作输入对话框时,其按钮不起作用 我使用了setRetainInstance(true) 及 但按钮不起作用 我的对话框片段代码是 public class TagDialogFragment extends DialogFragment { Dialog tagDialog=null; public static TagDialogFragment newInstance(String ti

在代码中,我创建了一个对话框片段,在搜索了不同的博客后,我能够在屏幕旋转后保留该对话框片段,但当我将其用作输入对话框时,其按钮不起作用

我使用了
setRetainInstance(true)

但按钮不起作用
我的对话框片段代码是

public class TagDialogFragment extends DialogFragment {
Dialog tagDialog=null;
 public static TagDialogFragment newInstance(String title) {
        TagDialogFragment frag = new TagDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    } 
public interface TagDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog,String tag);
    public void onDialogNegativeClick(DialogFragment dialog);
}
 // Use this instance of the interface to deliver action events
TagDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the TagDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the TagDialogListener so we can send events to the host
        mListener = (TagDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement TagDialogListener");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    //Use inflater to inflate the custom layout for our alert
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogLayout= inflater.inflate(R.layout.tag_dialog,null);
    final TextView entredTag=(TextView)dialogLayout.findViewById(R.id.tag);
    builder.setView(dialogLayout)
           .setTitle("Enter tag name for calculation")
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   String tag=entredTag.getText().toString();
                   mListener.onDialogPositiveClick(TagDialogFragment.this,tag);
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogNegativeClick(TagDialogFragment.this);
               }
           });
    // Create the AlertDialog object and return it
    tagDialog=builder.create();
    return tagDialog;
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}
}
case R.id.btntag:
         //create the input dialog
        if(Double.parseDouble(currentInput)!=0){
            tagDialog=TagDialogFragment.newInstance("tagDialog");
            tagDialog.show(getSupportFragmentManager(), "tagDialog");
        }
        break;
“我的活动”对话框中的被实例化为

public class TagDialogFragment extends DialogFragment {
Dialog tagDialog=null;
 public static TagDialogFragment newInstance(String title) {
        TagDialogFragment frag = new TagDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    } 
public interface TagDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog,String tag);
    public void onDialogNegativeClick(DialogFragment dialog);
}
 // Use this instance of the interface to deliver action events
TagDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the TagDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the TagDialogListener so we can send events to the host
        mListener = (TagDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement TagDialogListener");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    //Use inflater to inflate the custom layout for our alert
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogLayout= inflater.inflate(R.layout.tag_dialog,null);
    final TextView entredTag=(TextView)dialogLayout.findViewById(R.id.tag);
    builder.setView(dialogLayout)
           .setTitle("Enter tag name for calculation")
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   String tag=entredTag.getText().toString();
                   mListener.onDialogPositiveClick(TagDialogFragment.this,tag);
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogNegativeClick(TagDialogFragment.this);
               }
           });
    // Create the AlertDialog object and return it
    tagDialog=builder.create();
    return tagDialog;
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance())
        getDialog().setOnDismissListener(null);
    super.onDestroyView();
}
}
case R.id.btntag:
         //create the input dialog
        if(Double.parseDouble(currentInput)!=0){
            tagDialog=TagDialogFragment.newInstance("tagDialog");
            tagDialog.show(getSupportFragmentManager(), "tagDialog");
        }
        break;

有什么方法可以解决这个问题吗?

对于对话框片段的功能代码,我们需要重写onCreate()和onDestroyView()


检查旋转后“currentInput”的值感谢大家的支持,但问题不在currentInput变量中,但在搜索android API后,我解决了这个问题,实际上我正在通过@overriding OnCreateDialog创建对话框,需要实现请发布答案,它将帮助其他人在未来为对话框片段编写代码