Storyboard android back button

Hello guys! Help me please, i’m stuck with hardware android back button, i don’t understand why it’s doesn’t work and tried to find a solution for at least a week

I have three scenes: “listlesson”->“lesson1”->“help1”

And all works fine, expect this “hardware android back button”, - because here begin some strange things:

When i go to “listlesson”->“lesson1” and then push  “back button” - it works fine! But when i push it from scene “help1” - my app crashes, in “help1” i use same code as  in “lesson1”, i will be very glad if anyone can tell me why it’s happening  

Here is a code:

local function onKeyEvent( event )     local phase = event.phase;     local keyName = event.keyName;     if ( "back" == keyName and phase == "up" ) then         storyboard.gotoScene(true,  "listLesson" ,"fromLeft", 200)     end     return true    end      if system.getInfo( "platformName" ) == "Android" then  Runtime:addEventListener( "key", onKeyEvent ) end  

All display back button works fine, if anyone have ideas i will be very glad!!! I put two this file in attach

Change:

storyboard.gotoScene(true,  “listLesson” ,“fromLeft”, 200)

 

to

storyboard.gotoScene(“listLesson” ,“fromLeft”, 200)

Keep in mind, filenames on device are case sensitive.  You have “listLession” here, but in your text above you have “listlesson”.  Just make sure what you put in quotes matches the filename on disc.

It doesn’t help me:( And yes, of course i checked my scene name in quotes one hundred times for matches the filename and register and all right with it(

LIkely one of two possible issues. You can put in print statements to figure out which is the cause.

  1. This is evaluating to false:  if ( “back” == keyName and phase == “up” ) then

Put in a couple of prints to print out keyName and phase to make sure they are what you expect them to be and verify that the event handler is getting called.

  1. This is evaluating to false: if system.getInfo( “platformName” ) == “Android” then

Again some print statements will show you the problem.  Since your code in #1 looks like it was pretty much copied from the blog post, I’m going to be that #2 is the culprit since the blog post didn’t wrap the addEventListener like  you’re doing. 

Thank you! I found my mistake: there was a string below into scene “help1”, i delete it - and now it works fine)

function scene:exitScene( event )     storyboard.removeScene( "help1" ); end  

I’m also remake a code a little, in main.lua i put this code

Runtime:addEventListener('key', function (event)         if event.keyName == 'back' and event.phase == 'down' then             local scene = storyboard.getScene(storyboard.getCurrentSceneName())             if scene and type(scene.backPressed) == 'function' then                 return scene:backPressed()             end         endend);   

in other scenes this code

function scene:backPressed()     storyboard.gotoScene('here you scene name', 'slideRight', 200)     return true  end  

maybe it will be useful for someone :slight_smile:

Change:

storyboard.gotoScene(true,  “listLesson” ,“fromLeft”, 200)

 

to

storyboard.gotoScene(“listLesson” ,“fromLeft”, 200)

Keep in mind, filenames on device are case sensitive.  You have “listLession” here, but in your text above you have “listlesson”.  Just make sure what you put in quotes matches the filename on disc.

It doesn’t help me:( And yes, of course i checked my scene name in quotes one hundred times for matches the filename and register and all right with it(

LIkely one of two possible issues. You can put in print statements to figure out which is the cause.

  1. This is evaluating to false:  if ( “back” == keyName and phase == “up” ) then

Put in a couple of prints to print out keyName and phase to make sure they are what you expect them to be and verify that the event handler is getting called.

  1. This is evaluating to false: if system.getInfo( “platformName” ) == “Android” then

Again some print statements will show you the problem.  Since your code in #1 looks like it was pretty much copied from the blog post, I’m going to be that #2 is the culprit since the blog post didn’t wrap the addEventListener like  you’re doing. 

Thank you! I found my mistake: there was a string below into scene “help1”, i delete it - and now it works fine)

function scene:exitScene( event )     storyboard.removeScene( "help1" ); end  

I’m also remake a code a little, in main.lua i put this code

Runtime:addEventListener('key', function (event)         if event.keyName == 'back' and event.phase == 'down' then             local scene = storyboard.getScene(storyboard.getCurrentSceneName())             if scene and type(scene.backPressed) == 'function' then                 return scene:backPressed()             end         endend);   

in other scenes this code

function scene:backPressed()     storyboard.gotoScene('here you scene name', 'slideRight', 200)     return true  end  

maybe it will be useful for someone :slight_smile:

Greeting everyone. I need some help please.

Im trying to override the back button, but it always close the application.

Im using storyboard, it happens in every scene.

I put this onKeyEvent on Runtime on main.lua, and main.lua call the first storyboard named menu.lua.

[lua]

local function onKeyEvent( event )

  local phase = event.phase;

  if (phase==“up”) then

    – do everything based on the current scene

  elseif (phase==“down”) then

    – do nothing

  end

  return true

end

[/lua]

Is it possibly a crash? I didn’t see any error or crash report on ADB debug screen.

Im wondering about the “return true”, where should i place it?

Any suggestion? Thanks before.

 local function onKeyEvent( event ) &nbsp; local phase = event.phase; &nbsp; if (phase=="up") then &nbsp;&nbsp;&nbsp; -- do everything based on the current scene &nbsp;&nbsp;&nbsp; return true&nbsp; --\<------- note this change. &nbsp; elseif (phase=="down") then &nbsp;&nbsp;&nbsp; -- do nothing &nbsp; end &nbsp; return false&nbsp; --\<------ note this change end

Now I don’t know why you’re exiting.  The reason we return false in this case is for the other key buttons you are not handling like the volume buttons to get passed back to the OS.

Can you post the code you’re trying to use at line 4 above?

Thanks Rob. I wont handle volume button, so i’ll use your return false.

I have found the reason of exiting. It wasn’t the line 4, so i thought its okay not to post it.

As i wrote above, i have function onKeyEvent in main.lua. But i put it into Runtime listener somewhere else, say menu.lua.

I didn’t see this as a problem before, because i used print() syntax inside onKeyEvent and they were printing to console just fine.

Now i move the Runtime:addEvent Listener() to main.lua. Those print() working fine as before, but now the back button doesn’t close the app anymore.

Maybe the returning value were changed when they were cross-referenced?

I still don’t get this stuff. I’ll look more at it later… Sorry for the trouble :slight_smile:

You should have the function and the call to add the event listener in main.lua.  Can you post your whole listener function?

Yes i have done that thanks.

[lua]

local function androKeyEvent(event)

    local phase = event.phase

    local returnValue = false

   

    if(phase == “down”) then

        local keyName = event.keyName

        local currentScene = storyboard.getCurrentSceneName()

        local function toExit( event )

            if (“clicked”==event.action) and (event.index==1) then

                native.requestExit()

            end

        end

        local function toMenu( event )

            if (“clicked”==event.action) and (event.index==1) then

                storyboard.gotoScene( “menu”, { effect=“crossFade”, time=200 } )

            end

        end

       

        if ( “back” == keyName ) then

            returnValue = true

            if ( currentScene==“splash” ) then

                native.requestExit()

            elseif ( currentScene==“menu” ) then

                if ( storyboard.isOverlay ) then

                    storyboard.hideOverlay()

                else

                    local alert = native.showAlert( “”, “Sure to exit ?”, { “Yes”, “No” }, toExit )

                end        

            elseif ( currentScene==“game” ) then    

                if ( storyboard.isOverlay ) then

                    storyboard.hideOverlay()

                else

                  – still working on this part. lots of print() and other stuff, so i remove this

                  local alert = native.showAlert( “”, “Back to MENU ?”, { “Yes”, “No” }, toMenu )

                end

            end

        end

    end

   

    return returnValue   

end

if(device.isAndroid) then

    Runtime:addEventListener( “key”,androKeyEvent )

end

[/lua]

That’s my listener Rob, if you want to take a look.

It doesn’t close the app anymore. But still, something wrong?

If you handle the back button, you should return true, but for other buttons return false.

As for what’s going on, put in some print statements and look at your log.  Make sure you’re getting the scene name you’re expecting.  Make sure some other error condition isn’t happening.   I think it’s related to using your native.showAlert() call.  I’ve not seen apps ask that before.  Usually if I hit the back button I don’t want to be prompted. 

Greeting everyone. I need some help please.

Im trying to override the back button, but it always close the application.

Im using storyboard, it happens in every scene.

I put this onKeyEvent on Runtime on main.lua, and main.lua call the first storyboard named menu.lua.

[lua]

local function onKeyEvent( event )

  local phase = event.phase;

  if (phase==“up”) then

    – do everything based on the current scene

  elseif (phase==“down”) then

    – do nothing

  end

  return true

end

[/lua]

Is it possibly a crash? I didn’t see any error or crash report on ADB debug screen.

Im wondering about the “return true”, where should i place it?

Any suggestion? Thanks before.

 local function onKeyEvent( event ) &nbsp; local phase = event.phase; &nbsp; if (phase=="up") then &nbsp;&nbsp;&nbsp; -- do everything based on the current scene &nbsp;&nbsp;&nbsp; return true&nbsp; --\<------- note this change. &nbsp; elseif (phase=="down") then &nbsp;&nbsp;&nbsp; -- do nothing &nbsp; end &nbsp; return false&nbsp; --\<------ note this change end

Now I don’t know why you’re exiting.  The reason we return false in this case is for the other key buttons you are not handling like the volume buttons to get passed back to the OS.

Can you post the code you’re trying to use at line 4 above?

Thanks Rob. I wont handle volume button, so i’ll use your return false.

I have found the reason of exiting. It wasn’t the line 4, so i thought its okay not to post it.

As i wrote above, i have function onKeyEvent in main.lua. But i put it into Runtime listener somewhere else, say menu.lua.

I didn’t see this as a problem before, because i used print() syntax inside onKeyEvent and they were printing to console just fine.

Now i move the Runtime:addEvent Listener() to main.lua. Those print() working fine as before, but now the back button doesn’t close the app anymore.

Maybe the returning value were changed when they were cross-referenced?

I still don’t get this stuff. I’ll look more at it later… Sorry for the trouble :slight_smile: