Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch event with hardware back button on android?

Tags:

android

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 ?

like image 873
Damir Avatar asked Nov 11 '11 13:11

Damir


People also ask

What is hardware back button in Android?

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.

How do you handle back buttons?

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.

How do you get the back button event in flutter?

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.


2 Answers

you can do by this

  1. override the onBackPressed() method into your Activity like this way

    public void onBackPressed(){      // do something here and don't write super.onBackPressed() } 
  2. 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); } 
like image 97
Pratik Avatar answered Sep 24 '22 18:09

Pratik


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

like image 23
kaspermoerch Avatar answered Sep 22 '22 18:09

kaspermoerch