key processing for android physical back key using composer

Hi! need help on how you can use the physical back key to go in previous page using composer module.

I would do something like this:

local function onKeyEvent( event )    local phase = event.phase    local keyName = event.keyName    print( event.phase, event.keyName )    if ( "back" == keyName and phase == "up" ) then       if ( composer.getCurrentSceneName() == "menu" ) then          native.requestExit()       else          if ( composer.isOverlay ) then             composer.hideOverlay()          else             composer.gotoScene( "menu", { effect="crossFade", time=500 } )          end       end       return true    end    return false end Runtime:addEventListener( "key", onKeyEvent )

This assumes your app has a scene called “menu” that if they hit the back button you would want to exit to and that other scenes the back button would go to the menu scene.

Rob

Thank you, can I ask a question? where do I need to put the the code?? inside the create:scene or outside?

Thank you, can I ask a question? where do I need to put the the code?? inside the create:scene or outside?

I am done it works perfect!!! thanks Rob

sorry if I keep posting. I have another problem when I pressed the back button it works but when I did it to the other button the application exits.

First, I would put this in your main.lua.  You want this behavior app wide, so that’s the best place.

We can only catch the back button.  If you hit the home button or the button to bring up the running apps, we can’t trap that.  This is by design of Android.  The home button should background your app into a non-running state, but it will immediately return you to the device’s home screen.

Rob

Thank you Rob =)

I would do something like this:

local function onKeyEvent( event )    local phase = event.phase    local keyName = event.keyName    print( event.phase, event.keyName )    if ( "back" == keyName and phase == "up" ) then       if ( composer.getCurrentSceneName() == "menu" ) then          native.requestExit()       else          if ( composer.isOverlay ) then             composer.hideOverlay()          else             composer.gotoScene( "menu", { effect="crossFade", time=500 } )          end       end       return true    end    return false end Runtime:addEventListener( "key", onKeyEvent )

This assumes your app has a scene called “menu” that if they hit the back button you would want to exit to and that other scenes the back button would go to the menu scene.

Rob

Thank you, can I ask a question? where do I need to put the the code?? inside the create:scene or outside?

Thank you, can I ask a question? where do I need to put the the code?? inside the create:scene or outside?

I am done it works perfect!!! thanks Rob

sorry if I keep posting. I have another problem when I pressed the back button it works but when I did it to the other button the application exits.

First, I would put this in your main.lua.  You want this behavior app wide, so that’s the best place.

We can only catch the back button.  If you hit the home button or the button to bring up the running apps, we can’t trap that.  This is by design of Android.  The home button should background your app into a non-running state, but it will immediately return you to the device’s home screen.

Rob

Thank you Rob =)

Does Composer.isOverlay work?  I saw it in the code above and tried it, but it’s not returning true for me when I think it should.  I can’t find it in the documentation either. 

Is there another way to identify whether or not an overlay is shown?   Composer.getSceneName(“overlay”) seems to return the name of the overlay even if it’s hidden.  Thanks.

Yea, I think that .isOverlay may have been a relic that I was setting myself to know if I was in an overlay or not.  There isn’t an API call or public member variable that lets you get that.  I must have been setting it and unsetting it as I showed and hid overlays.

Rob

Thanks Rob, that’s what I ended up doing too…setting a variable to keep track.

Does Composer.isOverlay work?  I saw it in the code above and tried it, but it’s not returning true for me when I think it should.  I can’t find it in the documentation either. 

Is there another way to identify whether or not an overlay is shown?   Composer.getSceneName(“overlay”) seems to return the name of the overlay even if it’s hidden.  Thanks.

Yea, I think that .isOverlay may have been a relic that I was setting myself to know if I was in an overlay or not.  There isn’t an API call or public member variable that lets you get that.  I must have been setting it and unsetting it as I showed and hid overlays.

Rob