Android - Confirm app exit with toast

后端 未结 6 656
滥情空心
滥情空心 2020-12-08 23:33

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 ba

相关标签:
6条回答
  • 2020-12-09 00:22

    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();
    ...
    
    0 讨论(0)
  • 2020-12-09 00:23

    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()
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:24

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:25

    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();
    }
    
    0 讨论(0)
  • 2020-12-09 00:33

    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

    0 讨论(0)
  • 2020-12-09 00:34
    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题