Patriotic
Patriotic

Reputation: 2300

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.

Upvotes: 1

Views: 945

Answers (3)

Patriotic
Patriotic

Reputation: 2300

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
}

Upvotes: 1

Vitaliy  Ptitsin
Vitaliy Ptitsin

Reputation: 329

You can find answere heare. Or you can lock fragment beerwin Activity.

getSuportFragmentManager().findFragmentByTag(TAG_FRAGMENT_A).callback();

Upvotes: 1

diogojme
diogojme

Reputation: 2359

I suggest you to look about Bus or use BroadcastReceiver for do it, multiples callbacks can let your code coupled, and this is horrible for maintenence.

Upvotes: 1

Related Questions