16

I want to implement the back button functionality in my application. In application whenever I'm clicking on back button in middle my control is going to login page directly, so can someone tell me where to override onKeyDown() or onBackPressed() methods?

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Log.e("back key pressed","Back key pressed");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

because I'm writing this inside onCreate and outside onCreate also, but it's not working ......

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user1249134
  • 165
  • 1
  • 1
  • 7
  • In general, methods like `onKeyDown()` ,or `onBackPressed()` should be override(you can take advantage of IDE,i.e, Eclipse, to do so beautifully), so they should be out of `onCreate()`. Can you explain your problem exactly? What's wrong? – Huang Mar 05 '12 at 12:10
  • Look at this answer: http://stackoverflow.com/a/3558613/571353 – SERPRO Mar 05 '12 at 12:12
  • 1
    add @Override above the function ... ??? – mihail Mar 05 '12 at 12:14

4 Answers4

12

Depends on whether or not you want to support pre-Android 2.0 phones. The onBackPressed() method was added to Android 2.0 (API 5).

You may want to read this post on the Android Developer blog for details:

http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

zostay
  • 3,985
  • 21
  • 30
  • thanku man but where to write this method ????because i am trying to write this method inside onCreate and out side onCreate also but still control is not going inside the method can you help me out by simple answer??? – user1249134 Mar 05 '12 at 12:21
5

see below code. write outside the onCreate

  @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
  {  
         //replaces the default 'Back' button action  
         if(keyCode==KeyEvent.KEYCODE_BACK)  
         {  

                Intent intent = new Intent(currentActivity.this, RequiredActivity.class);
                finish();
                startActivity(intent); 

         }  
         return true;  
   }  
Dharmendra Barad
  • 949
  • 6
  • 14
4

Yes you can override that back button

public void onBackPressed() {

     Intent start = new Intent(currentclassname.this,which activity u want.class);
        startActivity(start);
        finishActivity(0);
        }

By this you can move on any activity. This is very easy and simple way

Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
1

If your concern is about removing the login activity from the history stack.

Execute finish(); in your login activity when you start any other activity from that

Sai mukesh
  • 712
  • 3
  • 9
  • 3-05 14:13:56.327: W/KeyCharacterMap(374): Using default keymap: /system/usr/keychars/qwerty.kcm.bin – user1249134 Mar 05 '12 at 14:17
  • HELLO friends my control is not coming inside OnBack method ?can any body help regrading this ???? – user1249134 Mar 05 '12 at 14:43
  • Intent edit = new Intent(getParent(), MyGroup.class); TabGroupActivity parentActivity = (TabGroupActivity)getParent(); parentActivity.startChildActivity("Add contacts", edit); – user1249134 Mar 05 '12 at 14:44