[Resolved] System event for back button

Android devices have back button which android users uses very frequently.But my game shuts down whenever back button is pressed, I tried using System Event like application Exit,suspend etc but of no use, .Is there any function or other system events to handle this problem?? [import]uid: 148046 topic_id: 30531 reply_id: 330531[/import]

Here is how we deal with the “hard” back button on android:

First, we decide what is the desired functionality of the back button in each scene.
Once we know how it should behave we have this piece of code in main.lua

backFunc = nil  
local function onKeyEventFunc( event )  
 local phase = event.phase  
 local keyName = event.keyName  
 if keyName == "back" or keyName == "menu" then  
 if phase == "up" and keyName == "back" then  
 if not backFunc then  
 os.exit()  
 else  
 backFunc()  
 end  
 end  
 return true  
 end  
  
 return false  
end  
onKeyEvent = onKeyEventFunc  
Runtime:addEventListener( "key", onKeyEvent )  

This code does:

  1. Ignore any “menu” clicks by catching them (returning true means it was handled by this function). In our games we simply don’t want this button working at all.
  2. For the “back” button we call a global function called backFunc if it’s not nil. If backFunc is nil we exit.
    Now in each scene we set the global backFunc variable in the enterScene function:
function scene:enterScene( event )   
 backFunc = function() storyboard.gotoScene( "scene\_main") end  
 .  
 .  
 .  
end  

This causes the “back” button to go back to the main menu scene.

in our main menu scene we do:

function scene:enterScene( event )  
 backFunc = nil  
 .  
 .  
 .  
end  

This causes the back function to exit the game if you press “back” from the main menu.

Alternatively if you really do not want to use a global variable you can create a seperate key event listener like we did in main.lua for each scene, just remember to remove it when exiting the scene… We found that having one listener for this kind of job is just easier and safer. I don’t often use global variables but for this I chose to. [import]uid: 80469 topic_id: 30531 reply_id: 122342[/import]

gtt…Thanks…It was very helpfull…now I know how to handle it…!!!:slight_smile: [import]uid: 148046 topic_id: 30531 reply_id: 122471[/import]

Here is how we deal with the “hard” back button on android:

First, we decide what is the desired functionality of the back button in each scene.
Once we know how it should behave we have this piece of code in main.lua

backFunc = nil  
local function onKeyEventFunc( event )  
 local phase = event.phase  
 local keyName = event.keyName  
 if keyName == "back" or keyName == "menu" then  
 if phase == "up" and keyName == "back" then  
 if not backFunc then  
 os.exit()  
 else  
 backFunc()  
 end  
 end  
 return true  
 end  
  
 return false  
end  
onKeyEvent = onKeyEventFunc  
Runtime:addEventListener( "key", onKeyEvent )  

This code does:

  1. Ignore any “menu” clicks by catching them (returning true means it was handled by this function). In our games we simply don’t want this button working at all.
  2. For the “back” button we call a global function called backFunc if it’s not nil. If backFunc is nil we exit.
    Now in each scene we set the global backFunc variable in the enterScene function:
function scene:enterScene( event )   
 backFunc = function() storyboard.gotoScene( "scene\_main") end  
 .  
 .  
 .  
end  

This causes the “back” button to go back to the main menu scene.

in our main menu scene we do:

function scene:enterScene( event )  
 backFunc = nil  
 .  
 .  
 .  
end  

This causes the back function to exit the game if you press “back” from the main menu.

Alternatively if you really do not want to use a global variable you can create a seperate key event listener like we did in main.lua for each scene, just remember to remove it when exiting the scene… We found that having one listener for this kind of job is just easier and safer. I don’t often use global variables but for this I chose to. [import]uid: 80469 topic_id: 30531 reply_id: 122342[/import]

gtt…Thanks…It was very helpfull…now I know how to handle it…!!!:slight_smile: [import]uid: 148046 topic_id: 30531 reply_id: 122471[/import]