Having trouble with a hardware back button for my app.
Everytime I press it - it closes the app both in the simulator and test device.
I’m following this older tutorial for key processing and updated it for composer
https://coronalabs.com/blog/2013/03/26/androidizing-your-mobile-app/
and added a bunch of prints to help troubleshoot whats going so wrong here.
local publicClass={} publicClass.onKeyEvent= function ( event ) local composer = require( "composer" ) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then if ( composer.currentScene == "splash" ) then print(composer.currentScene) print("Splash screen Close") native.requestExit() else if ( composer.isOverlay ) then print("close overlay") composer.hideOverlay() else local lastScene = composer.getSceneName("previous") local currentScene = composer.getSceneName("current") print( "previous scene name is", lastScene ) print( "current scene name is", currentScene ) if ( lastScene ) then composer.gotoScene( lastScene, { effect="crossFade", time=500 } ) else native.requestExit() end end end end if ( keyName == "volumeUp" and phase == "down" ) then local masterVolume = audio.getVolume() print( "volume:", masterVolume ) if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 audio.setVolume( masterVolume ) end return true elseif ( keyName == "volumeDown" and phase == "down" ) then local masterVolume = audio.getVolume() print( "volume:", masterVolume ) if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 audio.setVolume( masterVolume ) end return true end return false --SEE NOTE BELOW end return publicClass
Maybe I’m not removing the eventHandler correctly
In the console I’ll get a notification that the previous screen loads but then in immediately closes the app.
So if I’m on the menu scene and press the back hardware button
In the console I can see it shows splash.lua but then the app closes.
If I go a few more levels deeper ex. level1
I press the button once i get multiple out put in Console from printing the keyPhase
>down
>down
>up
then it immediately goes back to menu scene
and then back to my original (level1) scene and closes the app. almost instantly
I dont think my Event Listeners are stacking and maybe I’m not removing them correctly.
I got the info on adding the Event Listeners from an older similar thread which i didnt want to bring back from the dead
but it’shere for reference
--scene:show -- phase == did local keyhandler = require("keyhandler") Runtime:addEventListener("key", keyhandler.onKeyEvent) --scene:hide -- phase == will Runtime:removeEventListener("key", nil)
Any help or links to a tutorial on handling Event Listeners properly would be great thanks!
Even if I remove native.requestExit() from my code the back button still shuts down the app completely.