Polar Polar - 18 days ago 5
Java Question

Implementing a Callback for DialogFragment

From my

Recylerview Adapter
, there are multiple buttons in every Items,
What I want is to show the
DialogFragment as confirmation
when they are clicked and execute a different methods/actions when the
positiveButton
is clicked.

The problem is I don't know how can I have or how can I implement a callback when the
positiveButton
is clicked and can execute a different methods or actions according to what button is to be clicked on every items.

e.g.
On my Adapter Class

@Override
public void onBindViewHolder(final PageOnlineAdapter.TheViewHolder holder, final int position) {

holder.btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);

holder.btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
}


Here's my DialogFragment Class

public class ConfirmationDialog extends DialogFragment {

public static ConfirmationDialog newInstance(String title, String description) {
ConfirmationDialog frag = new ConfirmationDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("description", description);
frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String description = getArguments().getString("description");
return new AlertDialog.Builder(getActivity())
//.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setMessage(description)
.setPositiveButton("CONTINUE",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this positiveButton is clicked
}
}
)
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//How to tell when this negativeButton is clicked
}
}
)
.create();
}
}

Answer

An option would be to create an interface that is going to be implemented by the Adapter and passed to the dialogframe when creating it. The dialogframe could then invoke a certain method when positive is clicked and another one when negative is clicked :

public interface DialogCallback{

    void clickedPositiveStart();
    void clickedPositiveStop();
    void clickedNegativeStart();
    void clickedNegativeStop();
}




public class MyAdapter extends Adapter implements DialogCallback, View.OnClickListener{

   @Override
   protected void onClick(View view){
       DialogFragment newFragment =  null;
       String action;

       if(view.getId() == R.id.your_start_button){ 
           action = "start";
       }else if(view.getId() == R.id.your_stop_button){
           action = "stop";    
       }else if(..){
           ....
       }else{
           return;
       }

       newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT, this, action);
       newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG); 
   }

   @Override
   public void clickedPositiveStart(){
        // do something when positive is clicked
   }

   @Override
   public void clickedPositiveStop(){
        // do something when positive is clicked
   }

   @Override
   public void clickedNegativeStart(){
        // do something when negative is clicked
   }

   @Override
   public void clickedNegativeStop(){
        // do something when negative is clicked
   }

}


public ConfirmationDialog extends Dialogfragment{

    private DialogCallback callback;
    private String action;

    public static ConfirmationDialog newInstance(String title, String description, DialogCallback callback, String action) {
        this.callback = callback;
        this.action = action;
        ...
    }


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String title = getArguments().getString("title");
    String description = getArguments().getString("description");
    return new AlertDialog.Builder(getActivity())
            //.setIcon(R.drawable.alert_dialog_icon)
            .setTitle(title)
            .setMessage(description)
            .setPositiveButton("CONTINUE",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                           if(action.equals("start"){
                              callback.clickedPositiveStart();
                           } else if(action.equals("stop"){
                              callback.clickedPositiveStop();
                           }
                        }
                    }
            )
            .setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                           if(action.equals("start"){
                              callback.clickedNegativeStart();
                           } else if(action.equals("stop"){
                              callback.clickedNegativeStop();
                           }
                        }
                    }
            )
            .create();
     }
 }

}