the back key is android is not working ?

Hi team,

i am struggling with following code… not sure why is not working in the real device and working fine in simulator when i change “back” to “b” for testing :

[lua]

function onKeyEvent( event )

   local phase = event.phase

   local keyName = event.keyName

   print( event.phase, event.keyName )

   if ( “back” == keyName ) then

      Runtime:removeEventListener( “key”, onKeyEvent )

      print(“current scene is”, composer.getSceneName( “current” ))

      if ( composer.getSceneName( “current” ) == “levels” ) then

         native.requestExit()

      

      else 

        restartGame()

      end

   end

end

    Runtime:addEventListener( “key”, onKeyEvent )

[/lua]

any idea?

regards

Abdul

What does logcat say? Do you see the result of this print() in your logcat output?

print( event.phase, event.keyName )

You need to return “true” when handling the back key to tell the operating system that you are overriding it.  Otherwise, the Android operating system will do its default behavior and back out of your app.

ksan,

i am getting ( up, back)…

Joshua,

i tried to add " return true" and “return false” at the end of function , but it is still not working ??

Regards

Abdul

Well, our key event feature definitely works.  Try our “Hardware/KeyEvent” sample project that is included with the Corona Simulator.

You might have a Lua runtime error somewhere in your code, such as within your restartGame() function.  ksan is right.  You should double check the Android log via “adb logcat” for any Lua errors that are happening in your code.

And you still need a “return true” statement within your back key handling if block.  That is absolutely necessary.  The sample app I mentioned above demonstrates this.

I tried to see the console but there is no errors. In Simulator also works fine and the restartgame function works fine as well.

what do you mean by “return true” ?

  • does it meant to tell android not to exist the app ? then we kick some codes ??

–should I put it at the end of the function or within if statement.

regards

Abdul

Have a look at the documentation below…

   http://docs.coronalabs.com/api/event/key/index.html

When you return true from the key event’s Lua listener, that tells the system that you are overriding the key event and you do not want the operating system to perform its default operation.  For example, if you return true when the back key is pressed, that prevents the Android operating system from backing out of the app, which is the default action.  Another example would be returning true when a volumeUp/Down key is pressed which would prevent the operating system from adjusting the system volume.

Returning false or not returning anything at all indicates that you are not overriding the key event and that you want the operating system to perform its default operation.  And that’s the problem with your code.  You are not returning true when handing the “back” key event, so, the Android operating system does its default action and backs out of your app.

Thanks Joshua for your time to explain the matter in this detail. I appreciate that…

I will play with return and test the result. I hope I can get it work.

Regards

Abdulaziz

This what worked for me  … thanks all for your help 

[lua]

function onKeyEvent( event )

      local phase = event.phase

      local keyName = event.keyName

      print( event.phase, event.keyName )

      if ( “back” == keyName and phase == “up” ) then

        closeMe()

      end

      

      return true  

end

   

      Runtime:addEventListener( “key”, onKeyEvent )

      

[/lua]

What does logcat say? Do you see the result of this print() in your logcat output?

print( event.phase, event.keyName )

You need to return “true” when handling the back key to tell the operating system that you are overriding it.  Otherwise, the Android operating system will do its default behavior and back out of your app.

ksan,

i am getting ( up, back)…

Joshua,

i tried to add " return true" and “return false” at the end of function , but it is still not working ??

Regards

Abdul

Well, our key event feature definitely works.  Try our “Hardware/KeyEvent” sample project that is included with the Corona Simulator.

You might have a Lua runtime error somewhere in your code, such as within your restartGame() function.  ksan is right.  You should double check the Android log via “adb logcat” for any Lua errors that are happening in your code.

And you still need a “return true” statement within your back key handling if block.  That is absolutely necessary.  The sample app I mentioned above demonstrates this.

I don’t recommend that you *always* return true.  This would tell the Android OS that you’re overriding *all* keys.  For example, pressing the Volume Up/Down buttons on your device will no longer change the volume of your app since you’re overriding those keys too.

Since you’re app is only interested in the back key, I recommend that you only return true from within your “back” key if condition block and return false at the bottom of your function.

One more thing.  I also recommend that you use the “down” phase for the “back” key.  The reason is that some of the old Galaxy Tab tablets will not give you an “up” phase for the back key if you release the button after holding it down for about 2 seconds.  The reason they do this is because they use that button (in combination of another button) to take screenshots after holding it down for that long.  Just one of those Android gotchas.  :slight_smile:

Thanks Joshua :slight_smile:

i have implemented your recommendations and it worked perfectly … i appreciate your time and advice … perfect corona support.

Regards

Abdul

I tried to see the console but there is no errors. In Simulator also works fine and the restartgame function works fine as well.

what do you mean by “return true” ?

  • does it meant to tell android not to exist the app ? then we kick some codes ??

–should I put it at the end of the function or within if statement.

regards

Abdul

Have a look at the documentation below…

   http://docs.coronalabs.com/api/event/key/index.html

When you return true from the key event’s Lua listener, that tells the system that you are overriding the key event and you do not want the operating system to perform its default operation.  For example, if you return true when the back key is pressed, that prevents the Android operating system from backing out of the app, which is the default action.  Another example would be returning true when a volumeUp/Down key is pressed which would prevent the operating system from adjusting the system volume.

Returning false or not returning anything at all indicates that you are not overriding the key event and that you want the operating system to perform its default operation.  And that’s the problem with your code.  You are not returning true when handing the “back” key event, so, the Android operating system does its default action and backs out of your app.

Thanks Joshua for your time to explain the matter in this detail. I appreciate that…

I will play with return and test the result. I hope I can get it work.

Regards

Abdulaziz

This what worked for me  … thanks all for your help 

[lua]

function onKeyEvent( event )

      local phase = event.phase

      local keyName = event.keyName

      print( event.phase, event.keyName )

      if ( “back” == keyName and phase == “up” ) then

        closeMe()

      end

      

      return true  

end

   

      Runtime:addEventListener( “key”, onKeyEvent )

      

[/lua]