Android: How to Use Onbackpressed() with Toast

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:

  1. The Log doesn´t work because of a wrong filter in the logcat console
  2. The Toast dosn´t work because of the wrong passed context
  3. 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



Leave a reply



Submit