How to add alert dialog onKeyDown?
<p>I have this code on my application </p>

<pre><code>@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {


        Intent a = new Intent(this,a_stages.class);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(a);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}       
</code></pre>

<p>now I want to add an alert dialog that will ask the user if he/she wants to go, for example on another page, if the user clicks on yes, it will intent to a specific page and if the user clicks on cancel, dialog.cancel();.</p>

<p><strong>EDIT</strong>
I tried this code but I got an error on the line "Intent a = new Intent(this,a_stages.class);" that says "The constructor Intent(new DialogInterface.OnClickListener(){}, Class) is undefined"    </p>

<pre><code>@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(bq1.this);

        // Setting Dialog Title
        alertDialog.setTitle("Go back to home");

        // Setting Dialog Message
        alertDialog.setMessage("Are you sure you want to go back to home?");



        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {

                 Intent a = new Intent(this,a_stages.class);
                a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(a);
            }
        });

        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event

            dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();


    }
    return super.onKeyDown(keyCode, event);
}       
</code></pre>
