Return callback (open DialogFragment dialog box from CustomView)

This is related to my previous question: Open the DialogFragment dialog from CustomView

Now I need to use a callback to return a value from my dialog box. I understand that something like this is usually 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(); 

This is the case when you want an Activity to execute and receive a callback. But, if I want a custom view to do this (e.g. in my previous question)?

+1
source share
1 answer

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

-one
source

Source: https://habr.com/ru/post/1495344/


All Articles