Android back key event not working

On latest versions, the back key event has problems.

code is 

    function onKeyEventDirections( event )         print('onKeyEventDirections')         local phase = event.phase         local keyName = event.keyName           dump('',event,5)          if phase == "up" and keyName == "back" then             print 'directions key event'             local alert = native.showAlert( "Do you wish to quit?", "",                  { "Yes", "No" }, onExitD)                              return true         end            return false                 end     function onExitD( e )         print('onExit')         if "clicked" == e.action then             local i = e.index             if 1 == i then                 Runtime:removeEventListener( "key", onKeyEventDirections )                 print('yes exit')                 native.requestExit()    --    forces app to exit Android only                 return false             elseif 2 == i then                    print 'cancel exit'             end         end                  print ('on exit end')         return false     end          Runtime:addEventListener( "key", onKeyEventDirections )  

This works fine in version 1082, but not in version 1139.

Basically, hitting the back key just quits the app in version 1139, without calling onKeyEventDirections. In version 1082, function onKeyEventDirections is always called.

In another part of the code, we have another back key event that is working fine in version 1139, so its not like the functionality is totally gone

Is there anything wrong with the code above?

Has anyone else noticed this?

ok, have downloaded every recent version, and version 1138 is when the problem starts.

version 1137 and before are fine.

version 1138 to 1142 has problems with the ‘key’ event listener as detailed above.

My android key handlers worked, and still work fine (using 1139 now). But my handlers have a subtle difference:

[lua]

local function onAndroidKeyEvent(event)        – Handler for android Back and other keys…     
local retVal = false
    
    if( event ~= nil ) then
        local phase = event.phase
        local keyName = event.keyName
        print(“onAndroidKeyEvent() — (”…phase…" , " … keyName …")")
 
        if( (keyName == “back”) and (phase == “down”) ) then
            retVal = true
            timer.performWithDelay( 70, backArrow ) – So we leave event handler, and not during android key events / etc                                                    
        end
    end

    return retVal
end

[/lua]

we are finding on build 1138 onwards that the key event handler is just not being called - regardless of what is in the handler.

as soon as we build with 1137, the handler gets called.

There were changes to the key event system in 1138 it seems (from the daily build log for 1138):

  • Android: Rewrote “touch”, “tap”, and “key” event handling code in preparation for game controller support.
  • Android: Updated “key” event to support all keys and gamepad buttons.

I did have a few subtle problems when I did my last update (but not with keys). In one case, something I used to call wasn’t getting executed. Turned out, it wa a forward reference I never made a prototype for. Not sure why it suddenly became a problem (maybe from the lua engine update). Just adding a function prototype fixed it though. Along those lines, you have the forward reference to onExitD() from onKeyEventDirections() declared beforehand? (delcared like “local onExitD = {}”)

Other than that (and the “down” vs “up”, it looks pretty vanilla to me.

yes, the relevant routines are predeclared, e.g. local functionName at the beginning.

Strangely, we have two screens, one of which leads to the other, both with the same key handing code. 

The key handling works in the first, but doesn’t appear to register in the second. Both screens remove the event handler on exit. No errors are shown with ADB.

As I say, it works fine in version 1137 and before, just not in 1138 onwards, so something has changed.

in the second screen, it is never calling onKeyEventDirections, let alone onExitD.

The event handler ‘Runtime:addEventListener( “key”, onKeyEventDirections )’ just doesn’t register/execute, even though it is definitely called.

Just a quick question. You said you have the same code on two screens. That would mean your app doesn’t change pages when they hit the back key, but instead both pages pop up an “Exit?” dialog, right?

Just wanted to make sure I understood what it was doing (and that the other code is exactly the same, as opposed to one screen trying to go back to the other…)

Just to clarify, it has the same code but different function and variable names, to avoid any clash. The code is not shared between the pages.

There are many pages in the app.

One page (page A) works with the back button.

This page will also go forward to a second page (page B), where the back button does not work, so page B will not go back to page A.

It works fine in version 1137 and before.

ok, have downloaded every recent version, and version 1138 is when the problem starts.

version 1137 and before are fine.

version 1138 to 1142 has problems with the ‘key’ event listener as detailed above.

My android key handlers worked, and still work fine (using 1139 now). But my handlers have a subtle difference:

[lua]

local function onAndroidKeyEvent(event)        – Handler for android Back and other keys…     
local retVal = false
    
    if( event ~= nil ) then
        local phase = event.phase
        local keyName = event.keyName
        print(“onAndroidKeyEvent() — (”…phase…" , " … keyName …")")
 
        if( (keyName == “back”) and (phase == “down”) ) then
            retVal = true
            timer.performWithDelay( 70, backArrow ) – So we leave event handler, and not during android key events / etc                                                    
        end
    end

    return retVal
end

[/lua]

we are finding on build 1138 onwards that the key event handler is just not being called - regardless of what is in the handler.

as soon as we build with 1137, the handler gets called.

There were changes to the key event system in 1138 it seems (from the daily build log for 1138):

  • Android: Rewrote “touch”, “tap”, and “key” event handling code in preparation for game controller support.
  • Android: Updated “key” event to support all keys and gamepad buttons.

I did have a few subtle problems when I did my last update (but not with keys). In one case, something I used to call wasn’t getting executed. Turned out, it wa a forward reference I never made a prototype for. Not sure why it suddenly became a problem (maybe from the lua engine update). Just adding a function prototype fixed it though. Along those lines, you have the forward reference to onExitD() from onKeyEventDirections() declared beforehand? (delcared like “local onExitD = {}”)

Other than that (and the “down” vs “up”, it looks pretty vanilla to me.

yes, the relevant routines are predeclared, e.g. local functionName at the beginning.

Strangely, we have two screens, one of which leads to the other, both with the same key handing code. 

The key handling works in the first, but doesn’t appear to register in the second. Both screens remove the event handler on exit. No errors are shown with ADB.

As I say, it works fine in version 1137 and before, just not in 1138 onwards, so something has changed.

in the second screen, it is never calling onKeyEventDirections, let alone onExitD.

The event handler ‘Runtime:addEventListener( “key”, onKeyEventDirections )’ just doesn’t register/execute, even though it is definitely called.

Just a quick question. You said you have the same code on two screens. That would mean your app doesn’t change pages when they hit the back key, but instead both pages pop up an “Exit?” dialog, right?

Just wanted to make sure I understood what it was doing (and that the other code is exactly the same, as opposed to one screen trying to go back to the other…)

Just to clarify, it has the same code but different function and variable names, to avoid any clash. The code is not shared between the pages.

There are many pages in the app.

One page (page A) works with the back button.

This page will also go forward to a second page (page B), where the back button does not work, so page B will not go back to page A.

It works fine in version 1137 and before.