Sunday, 14 February 2016

Back button pressed

Today I'am learning the back key function in android which can include going back to previous activity. This can be achieved by following code.

@Override
public void onBackPressed(){
       // do something on back or go to previous activity via intent
}

Or this code can also be used instead of using intent.

@Override
public void onBackPressed(){
       super.onBackPressed();
}

or on the hard way one can use this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
   if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==0){
         finish();
         return true;
  }
 return super.onKeyDown(keyCode, event);
}


I have done the first one and is working for me perfectly.  

No comments:

Post a Comment