Override back button press from View or external class
<p>What I'm trying to do is intercept the Back button press from an external Class like this:</p>

<pre><code>public class AnswerView extends RelativeLayout {

    public AnswerView(Context context) {
        super(context);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i("AlleAntwoorden", "Back button pressed!");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
</code></pre>

<p>I'm adding it like this from my main Activity:</p>

<pre><code>RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
mainLayout.addView(new AnswerView(this));
</code></pre>
