1

I have just overide my back button in my code like this

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {

                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event)
          {

                 {
                       startActivity(new  Intent(context, MainDialog.class));
                       finish();

                return true;
            }
            return super.onKeyUp(keyCode, event);
        }

Now I am facing a weird problem, I have a edit text on the current Activity. At the time of editing when I am trying to close the virtual keyboard by pressing the back button . My Application finishes... But I just want to close the keyboard not the Avtivity.. Any suggestion to solve my issue..?

Thanks in advance, Tanmay


I have tried both 

       @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) 
        {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

               startActivity(new  Intent(context, MainDialog.class));
               finish();
               return true;
        }
        return super.onKeyUp(keyCode, event);
    }

and

        @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) 
        {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {

               startActivity(new  Intent(context, MainDialog.class));
               finish();
               return true;
        }
        return super.onKeyUp(keyCode, event);
    }

But no result. Any suggestion!!

Sampson
  • 265,109
  • 74
  • 539
  • 565
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • possible duplicat: http://stackoverflow.com/questions/2592037/is-there-a-default-back-keyon-device-listener-in-android – Praveen Nov 02 '10 at 05:20

2 Answers2

4

It seems you missed the if statement that catches the back key code. Try this:

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(new  Intent(context, MainDialog.class));
            finish();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
Cristian
  • 198,401
  • 62
  • 356
  • 264
0

One more solution probably, can we do like this...

I have an ImageButton by id buttonBack in xml I am getting hold of that on btnBack and adding an OnClickListener

In public void onClick function I call an intent of the class which is having another layout probably the previous one class...hope this works for all... thanks.

ImageButton btnBack = (ImageButton)this.findViewById(R.id.buttonBack);
    btnBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), Your.class);
                    startActivityForResult(myIntent, 0);

        }
    });
Mukunda
  • 1,593
  • 3
  • 26
  • 45