One of the issues on the issue tracker for my app JustChess is a comment about back button behavior. Technically, their problem is that if they press the back button to leave the game, the game is gone when they try to play again by choosing to play one or two players. This is a problem for the user because they accidentally push the back button a lot, and they don’t want to lose their current game. You can read the entire write up on Gitlab:

https://gitlab.com/alaskalinuxuser/app_JustChess/-/issues/17

While it is very possible to fix this behavior, it actually messes with the simple flow of the app to change that now. They requested that when the app is force closed and reopened (either manually or by restart) that the game persists. The game does persist if you go to the home screen and then return to the app (this is called resuming), but not when the app is actually closed, nor when you press the back button to exit the board and go back to choose how you want to play. To change that behavior now means having a dialog with the user where they can continue their game or start a new one.

Of course, that is still possible, but it wasn’t really something I wanted to implement at this time. It should be simple enough, essentially to save the board and whose turn it was, feeding it back to the game as a “new game” in a saved position. For now, I decided to use a simple trick to solve the “accidentally pressing the back button” issue:

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setTitle("Quit Game?")
                .setMessage("Are you sure you want to quit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        MainActivity.super.onBackPressed();
                    }
                }).create().show();
    }

Adding this block of code to the MainActivity.java file, there will now be a popup asking you if you want to quit when you press the back button. I know that “onBackPressed()” is depreciated, but this game’s API is low enough that it still works just fine.

Linux – keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *