Android - Confirm app exit with toast

2020-02-03 07:24发布

问题:

I'm new to Android development and I want it so when the user presses the back button on the main activity, a toast message appears with a "confirm exit by pressing the back button again" message. How would I do this? This is what I have so far:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Toast s = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
    s.show();
    wait();

    public boolean onBackPressed() {
        finish();    
    }
}

回答1:

I would just save the time of the backpress and then compare the time of the latest press to the new press.

long lastPress;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if(currentTime - lastPress > 5000){
        Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG).show();
        lastPress = currentTime;
    }else{
        super.onBackPressed();
    }
}

You can also dismiss the toast when the app the back press is confirmed (cred @ToolmakerSteve):

long lastPress;
Toast backpressToast;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if(currentTime - lastPress > 5000){
        backpressToast = Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_LONG);
        backpressToast.show();
        lastPress = currentTime;
    } else {
        if (backpressToast != null) backpressToast.cancel();
        super.onBackPressed();
    }
}


回答2:

private long mLastPress = 0;
const int TOAST_DURATION = 5000;
Toast onBackPressedToast;
@Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if (currentTime - mLastPress > TOAST_DURATION) {
        onBackPressedToast = Toast.makeText(this, R.string.press_once_again_to_exit, Toast.LENGTH_SHORT);
        onBackPressedToast.show();
        mLastPress = currentTime;
    } else {
        if (onBackPressedToast != null) {
            onBackPressedToast.cancel();  //Difference with previous answer. Prevent continuing showing toast after application exit.
            onBackPressedToast = null;
        }
        super.onBackPressed();
    }
}


回答3:

work perfectly in my case

private static long back_pressed;

@Override
public void onBackPressed()
{
        if (back_pressed + 2000 > System.currentTimeMillis()) 
               super.onBackPressed();
        else 
             Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
        back_pressed = System.currentTimeMillis();
}


回答4:

In newer version you can use the snackbar instead of toast.

import android.support.design.widget.Snackbar;
...
Snackbar.make(content, "Click BACK again to exit", Snackbar.LENGTH_SHORT).setAction("Action", null).show();
...


回答5:

After having to implement the same behaviour many a times, decided to go aboiut building a library for the same : DoubleBackPress Android Library. It provides many easy to use templates and the double back press behaviour without all the hassle.

Just do :

// set the ToastDisplay to be shown on FirstBackPress
FirstBackPressAction firstBackPressAction = new ToastDisplay().standard(this);

// set the Action on DoubleBackPress
DoubleBackPressAction doubleBackPressAction = new DoubleBackPressAction() {
    @Override
    public void actionCall() {
        finish();
    }
};

// setup DoubleBackPress behaviour
DoubleBackPress doubleBackPress = new DoubleBackPress()
        .withDoublePressDuration(3000)
        .withFirstBackPressAction(firstBackPressAction)
        .withDoubleBackPressAction(doubleBackPressAction);

Finally, override the onBackPressed with DoubleBackPress behaviour for the back press.

@Override
public void onBackPressed() {
    doubleBackPress.onBackPressed();
}

Example GIF for similar behaviour



回答6:

Best and simple solution with Toast

In Java

private Toast exitToast;

@Override
public void onBackPressed() {
    if (exitToast == null || exitToast.getView() == null || exitToast.getView().getWindowToken() == null) {
        exitToast = Toast.makeText(this, "Press again to exit", Toast.LENGTH_LONG);
        exitToast.show();
    } else {
        exitToast.cancel();
        finish();
    }
}

In Kotlin

private var exitToast: Toast? = null

override fun onBackPressed() {
    if (exitToast == null || exitToast!!.view == null || exitToast!!.view.windowToken == null) {
        exitToast = Toast.makeText(this, "Press again to exit", Toast.LENGTH_LONG)
        exitToast!!.show()
    } else {
        exitToast!!.cancel()
        finish()
    }
}