# Exit Behaviour on Android

**URL:** <https://discourse.processing.org/t/exit-behaviour-on-android/36356>\
**Category:** Processing for Android\
**Created:** [April 15, 2022, 4:24pm UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356 "2022-04-15T16:24:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [April 15, 2022, 4:24pm UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356/1 "2022-04-15T16:24:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rupesh\_Kumar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rupesh_kumar/32/12638_2.png) [@Rupesh\_Kumar](https://discourse.processing.org/u/Rupesh_Kumar)\
**Post date:** [May 12, 2022, 9:15pm UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356/2 "2022-05-12T21:15:30Z")

</div>

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

Rupesh

---

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [May 14, 2022, 10:15am UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356/3 "2022-05-14T10:15:08Z")

</div>

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:  
 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/b/4b1bbb9573808bfb1e4df2fa4f2ea819bcbbaeb1.gif)

In theory, I could recognize this press by writing

```auto
void keyPressed() {
  if(keyCode == 4) {
    //Code
  }
}

```

But the program closes immediately when I press this button.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 14, 2022, 2:09pm UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356/4 "2022-05-14T14:09:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [May 14, 2022, 2:28pm UTC](https://discourse.processing.org/t/exit-behaviour-on-android/36356/5 "2022-05-14T14:28:57Z")

</div>

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