“Android-izing” a Mobile App : back key

Hi,

I read this tutorial (thanks Rob)

http://coronalabs.com/blog/2013/03/26/androidizing-your-mobile-app/

I currently work on “Android-izing” my iOS App

  • I used onKeyEvent listener

  • I add the key callback Runtime:addEventListener( “key”, onKeyEvent )

  • I add a member variable to the storyboard table called returnTo

  • I set this .returnTo property in each scene’s enterScene() event function.

Unfortunately, When I press the back button (on a Nexus 7 or on a Galaxy Note) It doesn’t work.
wherever I am, app exits.

Any help would be great.

Here is my code in main.lua :

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if keyName == "back" and phase == "up" then if storyboard.currentScene == "scripts.menuScene" then native.requestExit() else local lastScene = storyboard.returnTo if ( lastScene ) then storyboard.gotoScene( lastScene, "fade", 250 ) else --native.requestExit() end end end if ( keyName == "volumeUp" and phase == "down" ) then local masterVolume = audio.getVolume() 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() if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 audio.setVolume( masterVolume ) end return true end return false end --add the key callback Runtime:addEventListener( "key", onKeyEvent )

Thanks

Olivier

You return true to “override” the key event.  If you don’t return true (ie: return false), then the OS will do its default behavior.  In the case for the back key, the OS will back out of your app.

Notice that you are *not* returning true when handling the back key event.  You need to return true when handling the back key and tell storyboard/composer to go to a previous scene.  When you are in the top level scene (such as the main menu for your app), then you could just simply return false and let the OS back out of your app… or return true and call native.requestExit() which effectively backs out of your app as well.

This information is documented here…

   http://docs.coronalabs.com/api/event/key/index.html

Thank you Joshua  :slight_smile:  !

Best

Olivier

I did what you said Joshua. It works, thank you very much!

But I have another problem :

When I’m in the top level scene and I press the Back button, I quit my app. Ok

But when I go back to my app again, this time, no more sound.

I Don’t understand why.

Thanks for your help

Best

Olivier

That is a bit odd.  I noticed that you are calling audio.setVolume().  Are you sure you’re not setting the volume to a low level by mistake somewhere?

Also, is there a reason why you want to override the volume levels yourself instead of letting the OS do it for you?

If you don’t return true for the volume keys, then they will set the OS will set the global media volume levels on your device, which is the default behavior.

Hi Joshua,

Thank you for your answer.

You’re right : there is no reason to override the volume levels by myself.

I changed my code with this :

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if keyName == "back" and phase == "up" then if storyboard.currentScene == "scripts.menuScene" then native.requestExit() else local lastScene = storyboard.returnTo if lastScene then storyboard.gotoScene(lastScene, "fade", 250) return true else native.requestExit() end end end return false end

Unfortunately, it still doesn’t work :

I’m in the top level scene and I press the Back button, I quit my app. 

When I go back to my app again, no more sound.

I then press the volume button : volume indicator is ok. Still no sound.

If I put the volume at the maximum value. Still no sound.

But I noticed that if I put the device on standby, and then reuse it, sound on my app comes back.

Thanks for your help

Best,

Olivier

I would like to clarify that

if I’m at the top level scene,

press the HOME button,

quit my app. 

and then go back to my app again,

the SOUND can be heard normaly.

So my problem is only with the back button…

Thanks for your help

Best,

Olivier

Hi,

I tried a lot of things (for example change sounds format to .ogg)

Still doesn’t work… :frowning:

Any idea?

Thanks

Olivier

I remember this being an issue a few months ago, but not anymore.  I just tried running an app with the newest daily build and audio still worked when backing out and going back into the app.  Perhaps you’re using an old Corona build?

Joshua,

you’re right!!!

I had an old Corona Build version (march 2014)

I’v just downloaded the last one and it works now!!!

Thank you so much!

Best regards

Olivier

You return true to “override” the key event.  If you don’t return true (ie: return false), then the OS will do its default behavior.  In the case for the back key, the OS will back out of your app.

Notice that you are *not* returning true when handling the back key event.  You need to return true when handling the back key and tell storyboard/composer to go to a previous scene.  When you are in the top level scene (such as the main menu for your app), then you could just simply return false and let the OS back out of your app… or return true and call native.requestExit() which effectively backs out of your app as well.

This information is documented here…

   http://docs.coronalabs.com/api/event/key/index.html

Thank you Joshua  :slight_smile:  !

Best

Olivier

I did what you said Joshua. It works, thank you very much!

But I have another problem :

When I’m in the top level scene and I press the Back button, I quit my app. Ok

But when I go back to my app again, this time, no more sound.

I Don’t understand why.

Thanks for your help

Best

Olivier

That is a bit odd.  I noticed that you are calling audio.setVolume().  Are you sure you’re not setting the volume to a low level by mistake somewhere?

Also, is there a reason why you want to override the volume levels yourself instead of letting the OS do it for you?

If you don’t return true for the volume keys, then they will set the OS will set the global media volume levels on your device, which is the default behavior.

Hi Joshua,

Thank you for your answer.

You’re right : there is no reason to override the volume levels by myself.

I changed my code with this :

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName if keyName == "back" and phase == "up" then if storyboard.currentScene == "scripts.menuScene" then native.requestExit() else local lastScene = storyboard.returnTo if lastScene then storyboard.gotoScene(lastScene, "fade", 250) return true else native.requestExit() end end end return false end

Unfortunately, it still doesn’t work :

I’m in the top level scene and I press the Back button, I quit my app. 

When I go back to my app again, no more sound.

I then press the volume button : volume indicator is ok. Still no sound.

If I put the volume at the maximum value. Still no sound.

But I noticed that if I put the device on standby, and then reuse it, sound on my app comes back.

Thanks for your help

Best,

Olivier

I would like to clarify that

if I’m at the top level scene,

press the HOME button,

quit my app. 

and then go back to my app again,

the SOUND can be heard normaly.

So my problem is only with the back button…

Thanks for your help

Best,

Olivier

Hi,

I tried a lot of things (for example change sounds format to .ogg)

Still doesn’t work… :frowning:

Any idea?

Thanks

Olivier

I remember this being an issue a few months ago, but not anymore.  I just tried running an app with the newest daily build and audio still worked when backing out and going back into the app.  Perhaps you’re using an old Corona build?

Joshua,

you’re right!!!

I had an old Corona Build version (march 2014)

I’v just downloaded the last one and it works now!!!

Thank you so much!

Best regards

Olivier