Override Android Back Button

A bit of information about why I am trying to do this: I use ActivityGroups to open activity from the tabHost activity and so that this new action remains under the tabs. I have this part. But when in this new action, if I use the back button, it takes me straight out of the activity of the tabs, so I have to click a few times to return to where I was.

Is there a way to set the back button to go to a specific action, and not to kill the current activity window?

+7
source share
2 answers

In general, I would advise this because it violates UX. The user expects the back button to kill the entire window, especially since you are using tabhost. To the user, the entire group (tabs and all) is one action that he wants to exit when he presses the "Back" button.

If you still want to do this, refer to # onBackPressed () . It is called when activity detects a click on a button from the back. By default, the operation is performed, but you can do whatever you want. I advise caution and caution.

Here you can find inspiration.

+5
source

I believe you should do something like this:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { // start new Activity here } return super.onKeyDown(keyCode, event); } 

But overriding the expected functionality of the back button is not recommended.

+7
source

All Articles