0

I want to call a method from a Fragment inside of the method setPositiveButton() of a AlertDialog used to return a Dialog for a DialogFragment and can't do that.

I have the method doSomething() inside a class named Test that extends of Fragment. Inside of this class I have a inner class that extends of DialogFragment. In the method onCreateDialog is where the problem happens. Look at the code:

public class Test extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.activity_main, null);
    return view;
}

public static class SelectDeviceDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
        .setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ((Test) getTargetFragment()).doSomething();
            }
        }).create();
    }
}

private void doSomething() {}
}

The DialogFragment and Fragment are from the lib android.support.v4.

The error happens because the cast to Test can't be done. How could I call the method doSomething in that case?

Thanks.

igorcadelima
  • 136
  • 9

1 Answers1

0

In revising my answer there are two routes you can go. The first is to change the static class to a final declaration like so:

public final DialogFragment selectDeviceDialogFragment = new DialogFragment(){
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                doSomething();
                            }
                        }).create();
    }    
};

If that is not suitable and you truly do need the static declaration, you should use an interface to communicate with it. Because you are using a static class, presume that you are calling this DialogFragment from another Fragment. If that is the case I would go this route:

public interface DoSomething{
     public void doSomething();
}

Now in your activity you need to implement the DoSomething interface:

private class MyActivity extends Activity implements DoSomething{

    /** other methods **/

    @Override
    public void doSomething() {
        FragmentManager fragmentManager = getFragmentManager();
        Test frag = (Test)fragmentManager.findFragmentById(/*the fragment id*/);
        frag.doSomething();
    }
}

Now back in your Test fragment in your inner dialog class:

public static class SelectDeviceDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                MyActivity activity = (MyActivity)getActivity();
                                activity.doSomething();
                            }
                        }).create();
    }
}

Hopefully this time I have answered your question :)

Christopher
  • 262
  • 2
  • 9
  • That cant't be done. I modified the question to better understanding. – igorcadelima Aug 27 '12 at 20:13
  • Hope the edit works! Please let me know if either of these solutions work for you – Christopher Aug 27 '12 at 22:24
  • Unfortunattely it doesn't work. The FragmentActivity is like this http://stackoverflow.com/questions/12127231/how-to-use-viewpager-to-switch-to-other-layout/, hence I don't have any Id identifying my fragments. – igorcadelima Aug 28 '12 at 00:54