Exit Popup on Android back button?

Currently, my app simply closes when the Android back button is hit. How can I display a popup aka Samsung’s recommendation?


Application is ended immediately without ‘Exit’ POPUP if it is ended by BACK Key while playing the game.
*It is impossible to move to previous page by BACK Key.


1. Execute application
2. Select game mode > Press BACK Key > Check the popup
3. Check the task manager


’Exit’ POPUP should be displayed if the application is ended by BACK Key while playing the game. [import]uid: 92238 topic_id: 35162 reply_id: 335162[/import]

PUT THIS IN THE FIRST STORYBOARD PAGE THAT IS CALLED…

[code]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
local function onKeyEvent( event )

local phase = event.phase
local keyName = event.keyName

if( (keyName == “back”) and (phase == “down”) ) then
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
native.cancelAlert( alert )
elseif 2 == i then
native.requestExit()
end
end
end
local alert = native.showAlert( “EXIT”, “ARE YOU SURE?”, { “NO”, “YES” }, onComplete )
group:insert(alert);

return true
end

– for default behavior, return false.
return false
end
Runtime:addEventListener( “key”, onKeyEvent );

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end
[/code] [import]uid: 185094 topic_id: 35162 reply_id: 143553[/import]

PUT THIS IN THE FIRST STORYBOARD PAGE THAT IS CALLED…

[code]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
local function onKeyEvent( event )

local phase = event.phase
local keyName = event.keyName

if( (keyName == “back”) and (phase == “down”) ) then
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
native.cancelAlert( alert )
elseif 2 == i then
native.requestExit()
end
end
end
local alert = native.showAlert( “EXIT”, “ARE YOU SURE?”, { “NO”, “YES” }, onComplete )
group:insert(alert);

return true
end

– for default behavior, return false.
return false
end
Runtime:addEventListener( “key”, onKeyEvent );

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end
[/code] [import]uid: 185094 topic_id: 35162 reply_id: 143553[/import]

PUT THIS IN THE FIRST STORYBOARD PAGE THAT IS CALLED…

[code]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
local function onKeyEvent( event )

local phase = event.phase
local keyName = event.keyName

if( (keyName == “back”) and (phase == “down”) ) then
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
native.cancelAlert( alert )
elseif 2 == i then
native.requestExit()
end
end
end
local alert = native.showAlert( “EXIT”, “ARE YOU SURE?”, { “NO”, “YES” }, onComplete )
group:insert(alert);

return true
end

– for default behavior, return false.
return false
end
Runtime:addEventListener( “key”, onKeyEvent );

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end
[/code] [import]uid: 185094 topic_id: 35162 reply_id: 143553[/import]

PUT THIS IN THE FIRST STORYBOARD PAGE THAT IS CALLED…

[code]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
local function onKeyEvent( event )

local phase = event.phase
local keyName = event.keyName

if( (keyName == “back”) and (phase == “down”) ) then
local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
native.cancelAlert( alert )
elseif 2 == i then
native.requestExit()
end
end
end
local alert = native.showAlert( “EXIT”, “ARE YOU SURE?”, { “NO”, “YES” }, onComplete )
group:insert(alert);

return true
end

– for default behavior, return false.
return false
end
Runtime:addEventListener( “key”, onKeyEvent );

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


end
[/code] [import]uid: 185094 topic_id: 35162 reply_id: 143553[/import]

Thank you. I’ll try this.

Thank you. I’ll try this.

I tried this code. It didn’t work for me. It had no effect on the back button.
I tried changing the default behavior to true & no effect.
 
– for default behavior, return false.
return false
 
Any help appreciated.

Please read this blog post.  I personally recommend putting your key handler in your main.lua so that if a scene gets removed you don’t loose your key handling, nor do you have to worry about adding and removing it on each scene call.

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

Many thanks folks. This is what worked for me. I added this code to main.lua.

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then local function onComplete( event ) if "clicked" == event.action then local i = event.index if 1 == i then native.cancelAlert( alert ) elseif 2 == i then native.requestExit() end end end local alert = native.showAlert( "EXIT", "ARE YOU SURE?", { "NO", "YES" }, onComplete ) group:insert(alert); return true end return true --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent )

At the end of the function, 

return false

Otherwise, the volume buttons won’t work & the app will be rejected by Samsung.

I tried this code. It didn’t work for me. It had no effect on the back button.
I tried changing the default behavior to true & no effect.
 
– for default behavior, return false.
return false
 
Any help appreciated.

Please read this blog post.  I personally recommend putting your key handler in your main.lua so that if a scene gets removed you don’t loose your key handling, nor do you have to worry about adding and removing it on each scene call.

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

Many thanks folks. This is what worked for me. I added this code to main.lua.

local function onKeyEvent( event ) local phase = event.phase local keyName = event.keyName print( event.phase, event.keyName ) if ( "back" == keyName and phase == "up" ) then local function onComplete( event ) if "clicked" == event.action then local i = event.index if 1 == i then native.cancelAlert( alert ) elseif 2 == i then native.requestExit() end end end local alert = native.showAlert( "EXIT", "ARE YOU SURE?", { "NO", "YES" }, onComplete ) group:insert(alert); return true end return true --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent )

At the end of the function, 

return false

Otherwise, the volume buttons won’t work & the app will be rejected by Samsung.