Press Back Twice To Exit Game - Android Back Button

Discussion in 'Buildbox General Discussion' started by maxcar, Mar 18, 2019.

  1. maxcar

    maxcar Boxer

    Joined:
    Sep 27, 2018
    Messages:
    23
    Likes Received:
    4
    Hi Boxers, I'm trying to implement a "press twice to exit" behavior for the back button on Android devices. I'm experiencing different results when pressing the back button on my game: sometimes it goes back to the previous screen, sometimes it exits the game and sometimes it does nothing and then opens a recent opened app :confused:.
    What I'm trying to achieve is having the back button display a toast message like "Press again to exit" on all my game screens, so I don't have to deal with the back button. I've seen this behavior in games like Wall Switch.

    I tried following the example below and modify my PTPlayer java file but I cannot get it working, and my Android knowledge is limited.
    Any help is appreciated. Thanks!

    Here's my code:

    Code:
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            this.hideVirtualButton();
    
            PTServicesBridge.initBridge(this, getString( R.string.app_id ));
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    
    // Press twice to exit
    
        private static final int TIME_LIMIT = 1500;
        private static long backPressed;
    
        @Override
        public void onBackPressed() {
    
            if (TIME_LIMIT + backPressed > System.currentTimeMillis()){
                super.onBackPressed();
            }
            else {
                Toast.makeText(getApplicationContext(), "Press again to exit",Toast.LENGTH_SHORT).show();
            }
            backPressed = System.currentTimeMillis();
    
        }
        
    Here's the example:
     
  2. Daywin

    Daywin Boxer

    Joined:
    Dec 16, 2017
    Messages:
    2
    Likes Received:
    0
    At least your app is responding something there is no results on the implimeting code.
     

Share This Page