No3*_*o3x 5 android callback android-fragments android-dialog android-dialogfragment
我尝试从a实现回调DialogFragment.有一个很好的例子,但他们不会DialogFragment从一个开放Fragment.
http://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中,我实现了接口,并以这种方式创建了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
    }
...
}
现在公共无效onAttach(SherlockActivity activity)的EditDateDialogFragment永远不会被调用,因为我创建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.我不确定如何更新对话框中的引用.
小智 10
刚刚跳到同样的问题,解决方案非常简单.而不是覆盖
public void onAttach(Context context) {}
覆盖这个:
public void onAttach(Activity activity) {}
现在一切都很好DialogFragment.
如何解决这个问题?
我猜你想要的RecordDetailFragment实例,表现为EditDateDialogListener为DialogFragment.如果是,则需要将其显式设置(并更新)为侦听器:
DialogFragment editDateFragment = EditDateDialogFragment.newInstance( recordId );
editDataFragment.setListener(RecordDetailFragment.this);
editDateFragment.show(getActivity().getSupportFragmentManager(), "EditDateDialogFrame");
这样setListener()的方法在哪里EditDialogFragment:
public void setListener(EditDateDialogListener listener) {
     mListener = listener;
}
例如,当用户旋转手机时,将重新创建活动及其片段,您需要重新设置监听器以指向新创建的RecordDetailFragment实例(您可能希望使用WeakReferencefor mListener).你可以在这个答案中找到类似的东西(你将在中找到两个片段onCreate).
编辑:在onCreate方法中Activity:
if (savedInstanceState != null) {
    RecordDetailFragment df = (RecordDetailFragment) getSupportFragmentManager().findFragmentByTag("rdf"); // "rdf" is the tag used when you add the RecordDetailFragment to the activity
    EditDateDialogFragment s = (EditDateDialogFragment) getSupportFragmentManager().findFragmentByTag("tag"); // "tag" is the string set as the tag for the dialog when you show it
    if (s != null) {
                   // the dialog exists so update its listener
        s.setListener(df);
    }
}
| 归档时间: | 
 | 
| 查看次数: | 6107 次 | 
| 最近记录: |