Android - Confirm app exit with toast

不问归期 提交于 2019-11-28 07:02:31

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();
    }
}
resource8218
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();
    }
}

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();
}

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();
...

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

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()
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!