If you have a requirement wherein you want to execute a method or a block of custom java code after the back button is pressed just before the back button action takes place, you need to Override the back button using onBackPressed() method.
package com.code2care.tools.sample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
Toast.makeText(MainActivity.this, "Back Button is being Pressed!", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}
Note: When the Back Button is being pressed you would see that the Toast Message is displayed and the Application quits.
If you wish that No Back Action is to be performed when the Back Button has clicked check this post: Disable Android Back Button Programmatically
- appcompat_v7 errors after updates to API level 21 Material Theme
- adb: The Android Debug Bridge and Commands
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- How to make TextView Text Transparent [Android]
- Android is starting optimizing... app 1 of 1
- How to make Text in TextView bold and italic in Android
- Android Development - How to switch between two Activities
- Android : IOException: Unable to open sync connection!
- Android Toolbar example with appcompat_v7 21
- How to Change Android Title Bar Color?
- How to Add Padding to Android TextView
- JavaScript : redirect page to other url
- Programmatically Send an Email from Android App using Intent
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- [Soluiton] You already have the latest version of Android Studio installed
- Detect swipes on Android Activity
- Add Buttons at the bottom of Android Layout xml file
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Hide Navigation Bar from Android Screen Activity
- Android Disable EditText from Auto Focus on Activity load
- Toast not getting displayed Android App
- How to Enable Developers Option in Android Phones Settings
- Android : Duplicate registration for activity com.example.abc
- Android ListView turns Black or Flickers while Scrolling
- Android Studio Ctrl Shift o auto import not working
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Python Hello World! Program with code example (snippet) - Python
- How to check the size of a Docker Container - Docker
- Adding internet permission to Android Project - Android
- Program 9: Divide Two Numbers - 1000+ Python Programs - Python-Programs
- Create React App using npm command with TypeScript - JavaScript
- Read a File Line By Line using Java 8 Stream API - Java
- Android Error Generating Final Archive - Debug Certificate Expired - Android