Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: intercepting back key
    primarykey
    data
    text
    <p>since the back key destroys my application and all data will be lost I need to intercept it to ask the user if that is really what he wants. </p> <p>I thought of the following structure:</p> <pre><code>@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) { // ask user if he really wants to exit // no --&gt; return true; // yes --&gt; return super.onKeyDown(keyCode, event); //manually entering either of the return values works fine } return super.onKeyDown(keyCode, event); } </code></pre> <p>The "ask user" piece I wanted to realize using an alert dialog. My problem is now that the alert dialog is displayed but that the onKeyDown method runs to the end while the alert dialog is shown and that within the alert dialog I have no idea how to tell the system to pass the right return value.</p> <p>The complete code I had in mind is</p> <pre><code>@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) { alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Tile"); alertDialog.setMessage("data lost, are you sure?"); alertDialog.setButton(-1, getString(R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; //I only can return without a boolean value here. } }); alertDialog.setButton(-2, getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); alertDialog.show(); } return super.onKeyDown(keyCode, event); } </code></pre> <p>Thanks, A.</p>
    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.
 

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