gpt4 book ai didi

android - onAttach() 从未在 DialogFragment 中调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:44 24 4
gpt4 key购买 nike

我尝试从 DialogFragment 实现回调。有一个很好的例子,但他们不会从 Fragment 打开此 DialogFragmenthttp://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

这是我的代码:

public class EditDateDialogFragment extends DialogFragment {
// Use this instance of the interface to deliver action events
EditDateDialogListener mListener;

/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface EditDateDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}


public static EditDateDialogFragment newInstance( int currentCategoryId ) {
EditDateDialogFragment p = new EditDateDialogFragment();
Bundle args = new Bundle();
args.putInt("currentRecordId", currentCategoryId);
p.setArguments(args);
return p;
}

@Override
public void onCreate(Bundle savedInstanceState) {
mCurrentRecordId = getArguments().getInt("currentRecordId");
super.onCreate(savedInstanceState);
}

public void onAttach(SherlockActivity activity) {

super.onAttach(activity);

try {
// Instantiate the EditDateDialogListener so we can send events to the host
mListener = (EditDateDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement EditDateDialogListener");
}

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
final View v = inflater.inflate(R.layout.fragment_dialog_edit_date, null);

return new AlertDialog.Builder(getActivity()).setTitle("Set Date...").setView(v).setCancelable(true).setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("", "Dialog confirmed");

mListener.onDialogPositiveClick(EditDateDialogFragment.this);

}
}).setNegativeButton("Abort", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("", "Dialog abort");
dialog.cancel();
}
}).create();
}
}

在 RecordDetailFragment.java 中,我实现了接口(interface)并以这种方式创建了 EditDateDialogFragment 的新实例(只是重要部分):

public class RecordDetailFragment extends SherlockFragment implements EditDateDialogFragment.EditDateDialogListener {
...
DialogFragment editDateFragment = EditDateDialogFragment.newInstance( recordId );
editDateFragment.show(getActivity().getSupportFragmentManager(), "EditDateDialogFrame");
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
LOGD(TAG, "Overriden Dialog confirmed");
//((EditDateDialogFragment) dialog).mDatePicker;

}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub

}
...
}

现在 EditDateDialogFragment 中的 public void onAttach(SherlockActivity activity) 永远不会被调用,因为我从 创建了 DialogFragment Fragment 而不是 Activity?如何解决这个问题?

更新:在 RecordDetailFragment 中,我将其插入到 onCreate()

if (savedInstanceState != null) {
EditDateDialogFragment dpf = (EditDateDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag("EditDateDialogFragment");
if (dpf != null) {
dpf.setListener((EditDateDialogListener) this);
}
}

我将 DialogFragment 的实例化更改为

 EditDateDialogFragment editDateFragment = EditDateDialogFragment.newInstance( recordId );
editDateFragment.setListener((EditDateDialogListener) this);
editDateFragment.show(getActivity().getSupportFragmentManager(), "EditDateDialogFragment");

请注意 EditDateDialogFragment 而不是 DialogFragment。我不确定如何更新对话框中的引用。

最佳答案

刚跳进同样的问题,解决方法很简单。而不是覆盖

public void onAttach(Context context) {}

覆盖这个:

public void onAttach(Activity activity) {}

DialogFragment 现在一切正常。

关于android - onAttach() 从未在 DialogFragment 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14070297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com