💾 Archived View for alaskalinuxuser.ddns.net › 2023-10-17.gmi captured on 2024-09-29 at 00:21:59. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

JustChess – Add quit dialog for back button presses.

">

12w, https://alaskalinuxuser3.ddns.net/wp-content/uploads/2023/10/device-2023-

10-16-123148-150x300.png 150w, https://alaskalinuxuser3.ddns.net/wp-content/

uploads/2023/10/device-2023-10-16-123148.png 720w" sizes="(max-width: 512px)

100vw, 512px" />

">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\n public void onBackPressed() {\n new AlertDialog.Builder(this)\n

.setTitle("Quit Game?")\n .setMessage("Are you sure you want to quit?")\n

.setNegativeButton(android.R.string.no, null)\n .setPositiveButton

(android.R.string.yes, new DialogInterface.OnClickListener() {\n\n public void

onClick(DialogInterface arg0, int arg1) {\n MainActivity.super.onBackPressed

();\n }\n }).create().show();\n }

">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.