# How to detect the back button action on mobile?

**URL:** <https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388>\
**Category:** Questions\
**Created:** [September 20, 2020, 5:47pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388 "2020-09-20T17:47:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Epitaph64](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/epitaph64/32/39288_2.png) [@Epitaph64](https://forum.defold.com/u/Epitaph64)\
**Post date:** [September 20, 2020, 5:47pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/1 "2020-09-20T17:47:59Z")

</div>

I see various threads on this, but nothing explaining the actual code used to capture the back button event. I tried comparing action\_id to KEY\_BACK and I tried creating a binding for the “Back” key in the input file. Neither method seems to respond to the back button. Does anyone know the proper way to do this?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 20, 2020, 8:17pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/2 "2020-09-20T20:17:58Z")

</div>

We have received reports that the back button behavior has changed on Android after the recent change where the engine is now running on a separate thread. In previous versions KEY\_BACK events were generated and could be captured by the game.

---

<div class="post-metadata">

**Author:** ![Epitaph64](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/epitaph64/32/39288_2.png) [@Epitaph64](https://forum.defold.com/u/Epitaph64)\
**Post date:** [September 20, 2020, 9:42pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/3 "2020-09-20T21:42:13Z")

</div>

Do you know of a way to clear logcat defold logs or perhaps watch the debug messages live while the app is running? I tried “adb logcat -s defold” but I have so many logs the command never returns (even if I leave it running for hours.)

EDIT: I setup dmengine.apk to debug it. I don’t see any specific action\_id or action properties that indicate that the back button is hit, just hash: [touch] and nil events… Wondering if it’s not possible to detect this for certain devices, etc with the current version of Defold?

EDIT2: Hmm, I think the action is captured, but even though my game saves on back button when I re-open it I get a black screen unless I manually kill the app and start it again. Also, it doesn’t seem to actually save if I exit this way… Not sure what’s going on =/

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 20, 2020, 10:03pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/4 "2020-09-20T22:03:11Z")

</div>

> [@Epitaph64](#):
>
> Do you know of a way to clear logcat

```
adb logcat -c

```

> **[Logcat command-line tool  |  Android Studio  |  Android Developers](https://developer.android.com/tools/logcat)**
>
> Logcat is a command-line tool that dumps a log of system messages, including stack traces, when the device throws an error and sends messages that you have written from your app with the Log class.

I can also recommend using PID Cat, a wrapper around logcat, to help with filtering and to show logs with color. [GitHub - JakeWharton/pidcat: Colored logcat script which only shows log entries for a specific application package. · GitHub](https://github.com/JakeWharton/pidcat)

---

<div class="post-metadata">

**Author:** ![Epitaph64](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/epitaph64/32/39288_2.png) [@Epitaph64](https://forum.defold.com/u/Epitaph64)\
**Post date:** [September 20, 2020, 10:05pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/5 "2020-09-20T22:05:43Z")

</div>

Hmm yeah I tried that. I think it did clear my logs but it takes an extremely long time to run adb logcat -s even though there are no logs now. Thanks for the advice, I’ll see if I can set that up maybe it will work better 😛

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [September 21, 2020, 7:39am UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/6 "2020-09-21T07:39:49Z")

</div>

> [@Epitaph64](#):
>
> I tried “adb logcat -s defold” but I have so many logs the command never returns (even if I leave it running for hours.)

That doesn’t clear the logs, only print them out as they come.

Also I’d suggest not using “-s defold” unless that’s what you want, since it doesn’t print out all info from the device (e.g from google related services).

I personally use [android-deploy.sh](https://github.com/defold/defold/blob/dev/scripts/mobile/android-deploy.sh) to launch apps and instead dig out the PID (process ID) to log all services under that PID.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 21, 2020, 8:19am UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/7 "2020-09-21T08:19:31Z")

</div>

> [@Mathias\_Westerdahl](#):
>
> I personally use [android-deploy.sh](https://github.com/defold/defold/blob/dev/scripts/mobile/android-deploy.sh) to launch apps and instead dig out the PID (process ID) to log all services under that PID.

I think this is what PID Cat does + color logging

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [September 21, 2020, 9:11am UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/8 "2020-09-21T09:11:04Z")

</div>

Correct, and here’s my personal [android\_deploy.sh](https://gist.github.com/mathiaswking/cd2f7cc4af95f77310b5817a33f9743a) with support for `pidcat`

---

<div class="post-metadata">

**Author:** ![Riad](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/riad/32/20518_2.png) [@Riad](https://forum.defold.com/u/Riad)\
**Post date:** [August 13, 2021, 4:19pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/9 "2021-08-13T16:19:45Z")

</div>

Hello @britzl

What’s the status of Back Button on Mobile?

I’m trying it on Android S9 Galaxy, it’s not responding. Could you please inform me if it’s under investigation? Can I hope for a fix soon?

Thanks a lot

Regards  
Riad

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [August 13, 2021, 5:32pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/10 "2021-08-13T17:32:48Z")

</div>

Hmm, I’ve just tested it on 3 phones and it works fine. Are you sure you added `KEY_BACK` in input bindings?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 13, 2021, 5:56pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/11 "2021-08-13T17:56:26Z")

</div>

> [@AGulev](#):
>
> I’ve just tested it on 3 phones and it works fine

Yes, we are not aware of any problems detecting the back button.

---

<div class="post-metadata">

**Author:** ![Riad](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/riad/32/20518_2.png) [@Riad](https://forum.defold.com/u/Riad)\
**Post date:** [August 14, 2021, 3:31pm UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/12 "2021-08-14T15:31:57Z")

</div>

Hello,

@AGulev , I have added KEY\_BACK as it should be…

After several tests I realized that it works only when I try the back button several times… which is weird, it’s like there is sensitive parameter applied to it, and I have to insist to make it work!

My back button works normally with other apps. Any idea what am I doing wrong?

Thanks and regards  
Riad

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 15, 2021, 5:13am UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/13 "2021-08-15T05:13:20Z")

</div>

What kind of phone do you have?

---

<div class="post-metadata">

**Author:** ![Riad](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/riad/32/20518_2.png) [@Riad](https://forum.defold.com/u/Riad)\
**Post date:** [August 16, 2021, 9:46am UTC](https://forum.defold.com/t/how-to-detect-the-back-button-action-on-mobile/66388/14 "2021-08-16T09:46:45Z")

</div>

Hello,  
It’s Samsung S9. I will try another brand and let you know asap.  
BR/Riad
