Advertisement
lvs

Android back button

lvs
Jul 5th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. -- this goes to your main.lua
  2. local _IS_ANDROID, _IS_IOS
  3. if platform == 'Android' then
  4.     _IS_ANDROID = true
  5. elseif platform == 'iPhone OS' then
  6.     _IS_IOS = true
  7. end
  8.  
  9. if _IS_ANDROID then
  10.     Runtime:addEventListener('key', function (event)
  11.             if event.keyName == 'back' and event.phase == 'down' then
  12.                 local scene = storyboard.getScene(storyboard.getCurrentSceneName())
  13.                 if scene and type(scene.backPressed) == 'function' then
  14.                     return scene:backPressed()
  15.                 end
  16.             end
  17.         end);
  18. end
  19.  
  20.  
  21.  
  22. -- and this goes to your storyboard scenes
  23. function scene:backPressed()
  24.     storyboard.gotoScene('scenes.menu', 'slideRight', 500) -- go to previous screen
  25.     return true
  26. end
  27.  
  28.  
  29. -- But in your main menu scene you should ask the user if he wants to exit on back button press
  30. -- I have this code:
  31. function scene:backPressed()
  32.     if not self.closeDialogShown then
  33.         self.closeButton._view._onRelease()
  34.     end
  35.     return true
  36. end
  37.  
  38. -- and close button in create scene:
  39.     self.closeButton = widget.newButton{
  40.         label = 'Exit',
  41.         onRelease = function ()
  42.                 self.closeDialogShown = true
  43.                 native.showAlert('Exit?', 'ARE YOU SURE YOU WANT TO EXIT?', {'NO', 'YES'}, function (event)
  44.                         self.closeDialogShown = false
  45.                         if event.index == 2 then
  46.                             if _IS_ANDROID then
  47.                                 native.requestExit()
  48.                             else
  49.                                 os.exit()
  50.                             end
  51.                         end
  52.                     end}
  53.             end}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement