Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add alert dialog onKeyDown?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload