Search code examples
androiddialogandroid-dialogfragment

How to receive result from a dialog fragment to a dialog fragment


I have a dialog A which is used in many activities. I create new dialog B and implemented a interface to get the call from A. In both case i used dialog fragment. For many Activities i can manage the callback from A using

if (getActivity() instanceof MyActivity) 
((MyActivity) getActivity()).manageSelectedItem();

But if dialog A is hosted in another dialog B then how to manage the callback.

I have searched and found solution based on Activity/FragmentActivity/Fragment on this but unable to solve the issue on DialogFragment.


Solution

  • I have solved the issue.I just implemented a interface in dialog B.Checks and initialize interface in onCreate method in dialog A whether the hosting dialog is activity /dialog.

    Here is the code

    Dialog DialogTwo as A :

    public class DialogTwo extends DialogFragment {
    
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            if(!(getActivity() instanceof SelectedItemListener)) {
                callback = (SelectedItemListener) getTargetFragment();
            }
        } catch (Exception e) {
            throw new ClassCastException("Calling Fragment must implement SelectedItemListener");
        }
    
    }
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogCustom));
    
        builder.setTitle(R.string.select_color)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (getActivity() instanceof SelectedItemListener) {
                                ((NewExerciseActivity) getActivity()).manageSelectedItem();
                        }else {
                            callback.manageSelectedItem();
                        }
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
    
        return builder.create();
       }
    }
    

    Call from Dialog B :

     private void showDialog() {
        FragmentActivity activity = (FragmentActivity) getActivity();
        FragmentManager fm = activity.getSupportFragmentManager();
        DialogTwo dialogTwo = new DialogTwo();
        dialogTwo.setTargetFragment(this,0);
        dialogTwo.show(fm, "dialogTwo");
    }
    
    @Override
    public void manageSelectedItem() {
           //do something
    }