Invoking a method when the back button is pushed on Android
<p>I am programming an Android application. I would like to invoke a certain method once the user pushes the "back" button. I want the program to work on all versions of Android, so I cannot use onBackPressed(). I tried the following piece of code (in the class MyView extends View):</p>

<pre><code>@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK &amp;&amp; event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

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

<p>But when I run it and press the back button, the program exits.
Is there a different way to treat the back button?</p>

<p>Thank you in advance.</p>
