How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
The hardware back button is found on most Android devices. In native applications it can be used to close modals, navigate to the previous view, exit an app, and more. By default in Ionic, when the back button is pressed, the current view will be popped off the navigation stack, and the previous view will be displayed.
This example demonstrates how do I handle back button in an android activity. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
By tapping the Android back-button (or the "pop" button) each square turns blue, one by one. Only when all squares are blue, tapping the back-button once more will return to the previous screen.
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){ // do something here and don't write super.onBackPressed() }
override the onKeyDown() method
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch(keyCode){ case KeyEvent.KEYCODE_BACK: // do something here return true; } return super.onKeyDown(keyCode, event); }
Override the method onBackPressed()
in whatever Activity
you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With