Skip to content Skip to sidebar Skip to footer

How To Receive The Actions Made By The User In A Dialogfragment?

I have a ListView that displays a list of Contacts. When a contact is clicked, a DialogFragment is created which displays a notice on whether or not to Cancel or Confirm. Based on

Solution 1:

Is there any way to relate the DialogFragment.show() and the Dialog button that was clicked?

Use an interface to pass that information to the activity which in turn will pass it to the adapter/fragment that has the possibility to do the change:

publicinterfaceOnSelectionMade {

     int OK = 1000;
     int CANCEL = 2000;
     doChange(int result);
}

Make the activity implement this interface and use the doChange() callback to do the change in the adapter, either by accessing the adapter directly(if you use a simple ListView) or pass it to the fragment holding the ListView to do the change:

publicclassYourActivityextendsActivityimplementsOnSelectionMade {

     @OverridepublicvoiddoChange(int result) {
         // the result parameter will be either the OK or CANCEL constants// you know now what button was clicked so you can do the change
         ...
     }

}

Now you need to wire up the DialogFragment to pass the event through OnSelectionMade like this:

privateOnSelectionMade mListener;

@OverridepublicvoidonAttach(Activity activity) {
    super.onAttach(activity);
    mListener = (OnSelectionMade) activity; // you'd want to implement a cast check here to be safe
}

//then in the onCreateDialog() method
builder.setPositiveButton("Confirm", newDialogInterface.OnClickListener() {
                   publicvoidonClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.OK); // do a null mListener check?
                   }
               })
               .setNegativeButton("Deny", newDialogInterface.OnClickListener() {
                   publicvoidonClick(DialogInterface dialog, int id) {
                       mListener.doChange(OnSelectionMade.CANCEL);
                   }
               });

You could probably pass a reference to the DialogFragment directly to pass the results, but that is not a very good solution long term.

Solution 2:

Indeed there is. In the Activity or Fragment from where you are instantiating your DialogFragment, first declare:

publicstaticfinalint MY_DIALOGFRAGMENT = 123;   /* Any random int values will do. */publicstaticfinalint CONTACT_CONFIRM = 124;
publicstaticfinalint CONTACT_DENY = 125;

Instantiate your DialogFragment like so:

FragmentManagerfm= getSupportFragmentManager();
MyDialogFragmentmyDialogFragment=newMyDialogFragment();
myDialogFragment.setTargetFragment(this, MY_DIALOGFRAGMENT);
myDialogFragment.show(fm, "my_dialog_fragment");

In the onCreateDialog() method of your DialogFragment,

builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Add the contactgetTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_CONFIRM, getActivity().getIntent());
                   dismiss();
               }
           })
           .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialoggetTargetFragment().onActivityResult(getTargetRequestCode(), 
                       MyActivity.CONTACT_DENY, getActivity().getIntent());
                   dismiss();
               }
           });

And finally, add the following method to your Activity or Fragment:

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case MY_DIALOGFRAGMENT:

                if (resultCode == CONTACT_CONFIRM) {

                    // Change color of listview item to green

                } elseif (resultCode == CONTACT_DENY){

                    // Change color of listview item to red

                }

                break;
        }
}

This should work. Try it.

Post a Comment for "How To Receive The Actions Made By The User In A Dialogfragment?"