How to detect back button on Android using native builds?

Hi,

I have to get info on android, in code ran from CoronaApplication, that the back button was pressed. Is there any way I could achieve this without extending CoronaActivity and changing AndroidManifest to use my custom version?

Thanks

Krystian 

The back button can be detected as a key event.

You’ll want to code this in the Lua part of your app.  You don’t need to do anything special for the native part of your code UNLESS you want to trigger something in that native code.

If that last bit is true, simply expose a function from your native code to Lua and call it in your key listener.

Here is some code to detect back button (and other stuff):

https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/android.lua 

Here is the relevant code cut out

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if( (keyName == "back") and (phase == "down") ) then -- DO SOMETHING HERE end return true end Runtime:addEventListener( "key", onKeyEvent );

Thanks, but there’s a reason why I want to have it done in native.

There are plenty of back button handlers in our project, depending on the scene, phase etc. I would need to add this in a lot of places to check if the native functionality is active, then send it. Also, I like to have the logic of each functionality contained within a module and not mix them.

I have overridden CoronaActivity and it still wouldn’t call the damn methods [not onKeyDown & not onBackButton] - and yeah, i did change android manifest to use my activity.

The back button can be detected as a key event.

You’ll want to code this in the Lua part of your app.  You don’t need to do anything special for the native part of your code UNLESS you want to trigger something in that native code.

If that last bit is true, simply expose a function from your native code to Lua and call it in your key listener.

Here is some code to detect back button (and other stuff):

https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/android.lua 

Here is the relevant code cut out

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if( (keyName == "back") and (phase == "down") ) then -- DO SOMETHING HERE end return true end Runtime:addEventListener( "key", onKeyEvent );

Thanks, but there’s a reason why I want to have it done in native.

There are plenty of back button handlers in our project, depending on the scene, phase etc. I would need to add this in a lot of places to check if the native functionality is active, then send it. Also, I like to have the logic of each functionality contained within a module and not mix them.

I have overridden CoronaActivity and it still wouldn’t call the damn methods [not onKeyDown & not onBackButton] - and yeah, i did change android manifest to use my activity.