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. 

Thanks to bvagdas who posted a solution at: http://forums.coronalabs.com/topic/29363-exit-popup-on-android-back-button/ but that didn’t work. Posting again since that thread is very old.

Does anyone know how to solve that problem?

Look at this blog post:

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

It should help you understand all of the Android specific behaviors you need to trap. 

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.

Look at this blog post:

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

It should help you understand all of the Android specific behaviors you need to trap. 

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.