Return a callback (Open a DialogFragment from within a CustomView)

自古美人都是妖i 提交于 2019-12-11 13:13:12

问题


This is related to my previous question here: Open a DialogFragment from within a CustomView

I now need to use a callback to return a value from my DialogFragment. I understand that something like this is commonly done:

public class MyDialogFragment extends DialogFragment {

public interface onMultipleSelectionFragmentCloseListener {
    public void onMultipleSelectionFragmentOkay();
}

onMultipleSelectionFragmentCloseListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (onMultipleSelectionFragmentCloseListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement onMultipleSelectionFragmentCloseListener");
    }
}
....
// to use it
mListener.onMultipleSelectionFragmentOkay();

That's the case when you want a Activity to implement and receive the callback. But, what if I want a custom view to do that (such as in my previous question)?


回答1:


You do the same thing- you create an interface like above. You keep a reference to a variable of that interface type. Then you have some function registerListener that takes a listener object and stores it so you can call it later.



来源:https://stackoverflow.com/questions/18072233/return-a-callback-open-a-dialogfragment-from-within-a-customview

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