Android: Proper Way to use onBackPressed() with Toast
I would implement a dialog asking the user if they wanted to exit and then call super.onBackPressed()
if they did.
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
WelcomeActivity.super.onBackPressed();
}
}).create().show();
}
In the above example, you'll need to replace WelcomeActivity with the name of your activity.
Clicking the back button twice to exit an activity
In Java Activity:
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
In Kotlin Activity:
private var doubleBackToExitPressedOnce = false
override fun onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed()
return
}
this.doubleBackToExitPressedOnce = true
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show()
Handler(Looper.getMainLooper()).postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
}
I Think this handler helps to reset the variable after 2 second.
How to implement press back twice to exit to ALL activities
You can make your own custom Activity
that extends AppCompatActivity
and put your implementation in there and then let each of your other activities extend that custom Activity
instead of AppCompatActivity
. this is not multiple inheritence
How to call a method when back button in emulator pressed
Try to run super method :
public void onBackPressed()
{
super.onBackPressed();
Toast.makeText(getApplication(),"A",Toast.LENGTH_SHORT).show();
}
It will call normal back press, and shows Toast
Android onBackPressed() is not being called?
This question is already answered, but I feel to clear something here in this topic. Most comments and answeres point out to use super.onBackPressed()
and that this is the cause of the not working method onBackPressed()
. But that is not correct and important to let other beginners know. The method onBackPressed()
does not need to use super.onBackPressed()
. onBackPressed()
also works if somebody, for example, comment super.onBackPressed()
out.
As the questionier has written, he won´t use super.onBackPressed()
because it will close the activity. So, the cause of this why it isn´t working, could be seperated into three possible causes:
- The Log doesn´t work because of a wrong filter in the logcat console
- The Toast dosn´t work because of the wrong passed context
- The OS is implemented wrong by the supplier.
Usually, the toast works by passing the correct context. In the case of questioner, simply passing this
.
@Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(this,"onBackPressed",Toast.LENGTH_SHORT).show();
}
For the Log, simply set the correct filter on logcat.
I don´t care if somebody give downvotes now, but it must be clear for other beginners, that super.onBackPressed()
must not be used.
Anyway, the use of onKeyDown()
also is a solution.
Ask user to exit application on back press in android?
You must set i = 0
when navigate back to HomeActivity
.
So, in your HomeActivity.java
set i = 0
inside OnResume()
Like this
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
i = 0;
}
Related Topics
Overlay Two Images in Android to Set an Imageview
How to Adjust Layout When Soft Keyboard Appears
How to Hide Action Bar Before Activity Is Created, and Then Show It Again
No Matching Client Found for Package Name (Google Analytics) - Multiple Productflavors & Buildtypes
Calculate Compass Bearing/Heading to Location in Android
Scroll Recyclerview to Show Selected Item on Top
Asynctask Won't Stop Even When the Activity Has Destroyed
How to Change Default Dialog Button Text Color in Android 5
Mapview in a Fragment (Honeycomb)
Find Distance Between Two Points on Map Using Google Map API V2
Record/Save Audio from Voice Recognition Intent
Push Notifications When App Is Closed
Android - Storing/Retrieving Strings with Shared Preferences
How to Send Data from One Fragment to Another Fragment
How to Determine If a Firebase User Is Signed in Using Facebook Authentication