Nitin Bathija
Nitin Bathija

Reputation: 810

AlertDialog Button needs double click

I am showing the progress dialog for downloading the file in my application, but if in case the user needs to cancel the download, then he will have to press the back button and then it will pop up alert dialog with two buttons. The problem is that I have to double click the alert dialog's buttons, and then only the alert dialog is dismissed. suggest me any solution for it.

here is peace of code for your reference..

@Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
        case progress_bar_type:
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            pDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if(keyCode == KeyEvent.KEYCODE_BACK){

                        running = false;
                        /*Intent intent = new Intent(context, NewDialog.class);
                        startActivity(intent);*/
                        AlertDialog.Builder  alertDialog = new AlertDialog.Builder(context);
                        alertDialog.setIcon(R.drawable.ic_launcher);
                        alertDialog.setTitle("Ariisto");
                        alertDialog.setMessage("Do you Want to Cancel the Download ?");
                        alertDialog.setCancelable(true);
                        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                File externalFile = new File(Environment.getExternalStorageDirectory(),"downloadedfile.pdf");
                                externalFile.delete();
                                pDialog.dismiss();
                                running = false;
                                Log.d("External File", "DELETED");
                                pDialog.setProgress(0);
                                count = 2;
                            }
                        });
                        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                new DownloadFileFromURL().execute(file_url);
                                running = true;
                                count = 0;
                            }
                        });
                        AlertDialog alert = alertDialog.create();
                        alert.show();
                    }
                    return false;
                }
            });

Upvotes: 1

Views: 1303

Answers (1)

Egor
Egor

Reputation: 40203

Problem is that overriding onKey() registers your Activity for two events, KEY_DOWN and KEY_UP for the given key. So it happens that you fire the AlertDialog twice, on both these events. I'd recommend you to override the onKeyDown() method and move your code there. Hope this helps.

Upvotes: 1

Related Questions