问题
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