Paul
Paul

Reputation: 5984

Android back key does nothing?

My app consists of one activity. When i press back nothing happens, why is this? I thought it would kill the app. If I press home the app contines in the background as desired. It's just the back button that does nothing. What could I have done to affect this?

I have read in the documentation:

public void onBackPressed ()

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity but you can override this to do whatever you want.

I have not overridden this.

Here is something that has been mentioned, overriding keys:

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event == null || event.getAction() == KeyEvent.ACTION_UP) {

        return false;
    }
   if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){

    mEntry.setOnFocusChangeListener(new OnFocusChangeListener(){
        @Override
        public void onFocusChange(View v,boolean hasFocus){
              /* When focus is lost check that the text field
               * has valid values.
               */
              if (!hasFocus && !mEntry.getText().toString().trim().equals("")) {

                    mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
                    mSession.appendToEmulator(cmdErase, 0, cmdErase.length);

                    Log.d(TAG, "in inner delete");
              }
              else {mEntry.setText(" ");
              }
        }          
    });


    Log.d(TAG, "in delete in delete in delete in delete");
    try {
        sendOverSerial("\b".getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

    return super.dispatchKeyEvent(event);
};

Another snippet, from onCreate:

mEntry = (EditText) findViewById(R.id.term_entry);

        mEntry.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {

                /* Ignore enter-key-up events. */
                if (event != null && event.getAction() == KeyEvent.ACTION_UP) {

                    return false;
                }

Upvotes: 5

Views: 861

Answers (1)

Thrakbad
Thrakbad

Reputation: 2738

Without your actual code it is diffucult to say, but I'll take a bold guess and say that somewhere in your Activity you have overridden onKeyDown or onKeyUp and KeyEvent.KEYCODE_BACK is handled there. I experienced the behaviour in my own app that if onKeyDown handles the back-key and returns true, onBackPressed() will never even get called.

Upvotes: 5

Related Questions