Exit Behaviour on Android

Hello, I’m just making an app for Android and have a few problems with the behavior as the app closes. My first problem is that the app closes directly when you press the back key. I want you to come to the menu first and only when another click is closed. The second problem is that the program restarts when changing the orientation of the screen. The last problem is that I do not have an exit function as on the computer to run code before closing. I know that you can close the program with System.exit(0), but I do not know how to override them because it is in the System class.

What kind of menu are you using here? Can you provide more information with a sample code? @TimeLex


I created the menu myself. Here’s a video of how it should behave if the “Back” button was Android’s built-in “Return” button:

In theory, I could recognize this press by writing

void keyPressed() {
  if(keyCode == 4) {

But the program closes immediately when I press this button.

My suggestion would be to use 3 pages: homePage, Page1, Page2 as well as a Quit button in the top right hand corner (outside of any of the pages ). I would take the Back button off of the home page and only display it on Page1 and Page2 and forget using the device’s “Return” button to switch pages. Hitting the “Quit” button would be trapped by mousePressed() and would call “exit();” to quit the app. MousePressed() would also trap hits on the pages/back buttons and appropriately switch from one page to the other.

Yes, that’s what I was planning to do, but it’s a pity that you can’t disable this behavior.