Luiz Alves Luiz Alves - 1 month ago 7
Android Question

Android Exception calling DialogFragment from Fragment

I have created a dialogfragment I´d like to call it from a Fragment.

I am getting an exception about MainActivity should implement the Listener.

But I implemented it into Fragment. What is wrong?

E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.techabout.appointment, PID: 4162
java.lang.ClassCastException: net.techabout.appointment.MainActivity@3a11f41 deve implementar MyDialogFragmentListener
at net.techabout.appointment.AndroidDialogFragment.onAttach(AndroidDialogFragment.java:53)
at

android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1043)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)


/////My Fragment

public class HorariosMedFragment extends Fragment implements
AndroidDialogFragment.MyDialogFragmentListener {

...........
......
void OpenDialog(String title, String msg, String but_sim, String but_nao) {
AndroidDialogFragment myDialogFragment = AndroidDialogFragment
.newInstance(title, msg, but_sim, but_nao);
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
//do something
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
dialog.dismiss();
}

}


//MyDialogFragment

public class AndroidDialogFragment extends DialogFragment {

public interface MyDialogFragmentListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}

// Usa essa instância da interface para entregar eventos de ação
MyDialogFragmentListener mListener;

static AndroidDialogFragment newInstance(String titulo, String msg,String but_sim, String but_nao) {
AndroidDialogFragment dialog = new AndroidDialogFragment();
Bundle args = new Bundle();
args.putString("title", titulo);
args.putString("but_sim", but_sim);
args.putString("but_nao", but_nao);
args.putString("msg", msg);

dialog.setArguments(args);
return dialog;
}


@Override
public void onAttach(Context context) {
super.onAttach(context);
// Verifica se a activity implementa a interface de callbacks
try {
// Instancia o MyDialogFragmentListener para que possamos enviar
// eventos para o host
mListener = (MyDialogFragmentListener) context;
} catch (ClassCastException e) {
// Essa activity não implementa a interface, levanta exceção
throw new ClassCastException(context.toString()
+ " deve implementar MyDialogFragmentListener");
}
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String but_sim = getArguments().getString("but_sim");
String but_nao = getArguments().getString("but_nao");
String msg = getArguments().getString("msg");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// Dispara o evento onDialogPositiveClick para a
// activity que estiver escultando
mListener.onDialogPositiveClick(
AndroidDialogFragment.this);
}
})
.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// Dispara o evento onDialogNegativeClick para a
// activity que estiver escultando
mListener.onDialogNegativeClick(
AndroidDialogFragment.this);
}
}).create();

return myDialog;
}

}

Answer

While creating Dialog Fragment instance set target fragment like

myDialogFragment.setTargetFragment(HorariosMedFragment.this,0);

After that in DialogFragment's onCreate method get that targeted fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
         mListener = (MyDialogFragmentListener) getTargetFragment();

    } catch (ClassCastException e) {
        throw new ClassCastException("Calling Fragment must implement MyDialogFragmentListener");
    }
}