Why isn't onShow in my custom dialogfragment b

2019-09-01 07:12发布

问题:

I have a custom alertdialog that implements interfaces so that their callbacks can be invoked.

I set breakpoints in the first lines of onDismiss and onShow.

The dialog pops up and I see all the views, but the breakpoint in onShow does not get hit and my async task does not get created.

public class CountdownDialogFragment extends DialogFragment 
implements OnClickListener, OnShowListener, OnDismissListener {
    InnerClassAsyncTask myAsyncTask;

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("Countdown");
        builder.setMessage("The final countdown...");

        builder.setNegativeButton("Cancel", this);
        builder.setPositiveButton(getResources().getString("Start", this);

        builder.setView(view);

        return builder.create();
    }
    @Override public void onDismiss(final DialogInterface dialog) {
        myAsyncTask.cancel(true);
    }

    public void setDialogOnClickListener(OnClickListener  
                                     dialogOnClickListener) {
        clickListener = dialogOnClickListener;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(clickListener != null) {
            clickListener.onClick(dialog, which);
        }
    }

    @Override
    public void onShow(DialogInterface dialog) {
        myAsyncTask = new InnerClassAsyncTask();
        myAsyncTask.execute();
    }

    private static class InnerClassAsyncTask extends AsyncTask<Void,    
                                                     Integer, Void> {
    // ...updates a determinate progressbar in this alert dialog
    }

In my calling fragment I instantiate this dialog via newInstance and show it via:

myCountdownDialog.show();

回答1:

in onCreateDialog, I forgot to set the listener: dialogObj.setOnShowListener(this).