Handle the BACK key event in the child view, not in the action

In my application, users can use thumbnail images to view the full size version. When the thumbnail is displayed, a bunch of new views in code (i.e., no XML) are created, added at the end of the view hierarchy, and some scaling and rotating transitions occur, then a full-size high-res version of the image is displayed. Clicking the full size image cancels the transitions and removes the new views from the view hierarchy.

I want users to also be able to press the BACK key to undo transitions between images. However, I can't seem to catch the KeyEvent. This is what I am 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;
            }
        });

EDIT: I had several answers from people providing snippets of code to be used in a class that extends Activity. This code, which I copied above, exists in a class that is created in multiple activities; my goal is to have code that listens for and captures the BACK key event in one place (like OnClickListener ()) instead of having it in every activity (and create long, cumbersome links from each activity to the zoomed object) ...

+3


source to share


2 answers


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//Your logic or stuff
return true;
}
else
return super.onKeyUp(keyCode, event);
}

      

Use the overide method described above to handle the Back key event

In this scenario, use as below 1) create an action as shown below:

/**
 * Top Activity that extends all Activity
 */
package com.com.com;//change your package name

import android.app.Activity;
import android.view.KeyEvent;


public class TopActivity extends Activity {
    protected BaseActivity _activity;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //your stuff if you wanna to have anything
            return true;
        }
        else
            return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            //your stuff if you wanna to have anything
            return true;
        }
        else
            return super.onKeyUp(keyCode, event);
    }

}

      



2) Now, in your code, each of your activities extends Acitivity, so change it to extend TopActivity like below:

public class YourActivity extends TopActivity {

      

// Your normal code for each action ...}

thats it ... Try it now

+4


source


Try to use

public class mActivity extends activity {



@Override
public void onBackPressed() {
    // Stuff
}

      

}

0


source







All Articles