Android back button action

Can I set the default back button action on another button? I mean, without writing code, is there a predefined Back method?

Thanks!

+4
source share
2 answers

onClick of the button add onBackPressed();

 public void onClick(){ onBackPressed(); } 
+11
source

There are two ways for your purpose:

first: Override the onBackPressed method in Activty :

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


2nd: override onKeyDown and find KeyEvent.KEYCODE_BACK

 public boolean onKeyDown(int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK) { } return false; } 


If you want to combine both (let's say you want BackAction on the menuButton button, it will look like this:

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { onBackPressed(); } return false; } 
+1
source

All Articles