I have a bunch of composer.* examples here: https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip
I just added a new example doing basically what you want to do with a module.
Download from the above link and see example 15_android_back_nav.
When you poke through the scene files (found in ifc/ folder), look for the marker: BACK_HANDLER
See the module android.lua for how I did this.
To use the module, do the following (in your scene files)
Normal Scene File
-
Somewhere towards top of file:
local android = require “android” local backHandler
2a. In create() method of your scene, If you intend to exit app from this scene:
if( not backHandler ) then backHandler = android.new( { debugEn = true, title = "Quit Game?", msg = "This will exit the game.\n\nAre you sure you want to quit?" } ) end
2b. In create() method of your scene, If you intend to return to another scene:
-- This assumes onBack is a function that calls the right composer functions to go back to another -- scene from this one -- -- AND... that onBack has already been defined by this point -- if( not backHandler ) then backHandler = android.new( { debugEn = true, onBack = onBack } ) end -- Tip: debugEn true enables the escape key so you can test this in the simulator
-
In the ‘did’ phase of show() method for your scene.
backHandler:activate() – BACK_HANDLER
-
In the ‘will’ phase of hide() method for your scene.
backHandler:deactivate() – BACK_HANDLER
Overlays
Overlays are a little trickier because they don’t trigger the will or did phases of the scene that is being overlayed. So you have to know this and act accordingly.
This example shows how to handle this, and I won’t show you here. Just look for the markers: BACK_HANDLER
Warning
My scene files are a LITTLE different from the ones Corona shows. I have split the show() and hide() methods into:
- willShow()
- didShow()
- willHide()
- didHide()
I do this because I like my code kept clearly separate.
Examine my samples carefully (see code at very bottom) and you’ll see how I did this.