How to listen for FragmentDialog clicks in the MainActivity if it cannot extend from FragmentActivity?

I am trying to show an initial dialog in my application so that the user accepts the Terms and Conditions and the Privacy Policy, setting from there a variable in SharedPreferences.

I've followed the steps here to pass the dialog events to the host, which in this case would be MainActivity.

The dialog is created fine, but the clicks are still heard on the Fragment, not on the host.

The only difference from my code is that in the Android example, MainActivity is made to extend from FragmentActivity:

public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{

In my case I can't do it, because my MainActivity already extends from AppCompatActivity.

What can I do to listen for the clicks of my FragmentDialog in my MainActivity?

This is my code:

MainActivity

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity  implements AcceptFragment.AcceptDialogListener
        {

            public void showNoticeDialog() {
                // Create an instance of the dialog fragment and show it
                DialogFragment dialog = new AcceptFragment();
                dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
            }

            // The dialog fragment receives a reference to this Activity through the
            // Fragment.onAttach() callback, which it uses to call the following methods
            // defined by the NoticeDialogFragment.NoticeDialogListener interface
            @Override
            public void onDialogPositiveClick(DialogFragment dialog) {
                //I have debugged and it does not enter here
                String a="s";
                // User touched the dialog's positive button
        //...
            }

            @Override
            public void onDialogNegativeClick(DialogFragment dialog) {
                //I have debugged and it does not enter here
                String a="s";

                // User touched the dialog's negative button
        //...
            }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showNoticeDialog();
    }

// ...
}

AcceptFragment

This is the code for the FragmentDialog. In the debug the clicks are being listened to in this class, not in MainActivity:

public class AcceptFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = requireActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                // Add action buttons
                .setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //Clicks are received here
                        String a="s";
                        // sign in the user ...
                    }
                })
                .setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //Or clicks are received here
                        String a="s";
                        AcceptFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();

    }

    public static String TAG = "AcceptFragment";

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

    // Use this instance of the interface to deliver action events
    AcceptDialogListener listener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            listener = (AcceptDialogListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(getActivity().toString()
                    + " must implement NoticeDialogListener");
        }
    }


}

Solution 1:

This is the code for the FragmentDialog. In the debug the clicks are being listened to in this class, not in MainActivity:

Doesn't look like you're invoking your listener.

.setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //Clicks are received here
                    String a="s";
                    // sign in the user ...

                    // INVOKE LISTENER
                    listener.onPositiveButtonClick(...);
                }
            })
            .setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //Or clicks are received here
                    String a="s";
                    AcceptFragment.this.getDialog().cancel();

                    // INVOKE LISTENER
                    listener.onNegativeButtonClick(...);
                }
            });