Why isn't onShow in my custom dialogfragment being invoked?

守給你的承諾、 提交于 2019-12-13 07:28:23

问题


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).



来源:https://stackoverflow.com/questions/28522103/why-isnt-onshow-in-my-custom-dialogfragment-being-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!