Handle BACK key event in child view
Posted
by
Mick Byrne
on Stack Overflow
See other posts from Stack Overflow
or by Mick Byrne
Published on 2012-04-03T04:00:19Z
Indexed on
2012/04/03
5:29 UTC
Read the original article
Hit count: 222
android
|android-view
In my app, users can tap on image thumbnails to see a full size version. When the thumbnail is tapped a bunch of new views are created in code (i.e. no XML), appended at the end of the view hierarchy and some scaling and rotating transitions happen, then the full size, high res version of the image is displayed. Tapping on the full size image reverses the transitions and removes the new views from the view hierarchy.
I want users to also be able to press the BACK key to reverse the image transitions. However, I can't seem to catch the KeyEvent. This is what I'm trying at the moment:
// Set a click listener on the image to reverse everything frameView.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { zoomOut(); // This works fine } }); // Set the focus onto the frame and then set a key listener to catch the back buttons frameView.setFocusable(true); frameView.setFocusableInTouchMode(true); frameView.requestFocus(); frameView.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // The code never even gets here !!! if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { zoomOut(); return true; } return false; } });
© Stack Overflow or respective owner