How to execute action after DialogFragment positive button clicked

Raffo picture Raffo · Apr 12, 2013 · Viewed 8.6k times · Source

I created the following DialogFragment deriving it from the Android documentation:

public class PayBillDialogFragment extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        final Bundle b = this.getArguments();
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Paga bollettino")
               .setPositiveButton("Paga", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!


                   }
               })
               .setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();

    }





}

From another Fragment (a ListFragment), when a row of the list is clicked the DialogFragment should be opened and after pressing the positive button of the DialogFragment I want to be able to remove the selected row of the ListFragment and also to call a method to perform a remote action associated to the removal. I implemented the ListFragment as follows:

public static class ListFragment extends android.support.v4.app.ListFragment {



        ArrayList<String> listItems=new ArrayList<String>();


        ArrayAdapter<String> adapter;


        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.list_fragment_view,
                    container, false);


            ListView lv = (ListView)rootView.findViewById(android.R.id.list);

            }});
            adapter=new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_list_item_1,
                    listItems);
                setListAdapter(adapter);
            return rootView;
        }



        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {



            //opening the dialogfragment


        }


    }




    }

What I don't know is how to handle the action after the click of the positive button of the DialogFragment. Can you help me?

EDIT: to clarify, this is the workflow: click on the list -> show the DialogFragment -> after click on DialogFragment remove the element from the list.

Answer

rciovati picture rciovati · Apr 18, 2013

This is how I handle communication between fragment and dialog fragment

Example fragment:

public class MainFragment extends Fragment {

    private static final int REQ_CODE = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment, container, false);
        Button b = (Button) v.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyDialog dialog = new MyDialog();
                dialog.setTargetFragment(MainFragment.this, REQ_CODE);
                dialog.show(getFragmentManager(), "dialog");
            }
        });
        return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(getActivity(), "Result: " + resultCode,
                Toast.LENGTH_SHORT).show();
        super.onActivityResult(requestCode, resultCode, data);
    }

}

Example DialogFragment:

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("My dialog message")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        notifyToTarget(Activity.RESULT_OK);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                notifyToTarget(Activity.RESULT_CANCELED);
                            }
                        });
        return builder.create();
    }

    private void notifyToTarget(int code) {
        Fragment targetFragment = getTargetFragment();
        if (targetFragment != null) {
            targetFragment.onActivityResult(getTargetRequestCode(), code, null);
        }
    }

}

This is the only method I got working also when changing orientation.