Here is my code for anyone that is looking to override the back button
Java code
//This boolean should go just after you create your activity (outside of the fragment) private boolean overrideBackButton = false; //This block of code should be inside your fragment //Lua to Java listener mBigCoronaView.setCoronaEventListener(new CoronaEventListener() { @Override public Object onReceivedCoronaEvent(CoronaView view, Hashtable\<Object, Object\> event) { final String eventName = (String) event.get("eventName"); if(eventName.equals("backButtonOverride")){ overrideBackButton = (Boolean) event.get("value"); } return ""; } }); //This override should also be outside of your fragment @Override public void onBackPressed() { //See if we should override back button if(overrideBackButton){ //Send back button event to lua Hashtable\<Object, Object\> event = new Hashtable\<Object, Object\>(); event.put("name", "backButton"); mBigCoronaView.sendEvent(event); return; } super.onBackPressed(); }
Lua Code
--//When you want to override the back button send this event Runtime:dispatchEvent({ name = "coronaView", eventName = "backButtonOverride", value = true }) --//When you don't want to override the back button (back button will close the app) send this event Runtime:dispatchEvent({ name = "coronaView", eventName = "backButtonOverride", value = false }) --//This should go in your main.lua --//This function handles what to do when the back button is pressed local function handleBackButton(event) print("Back button overriden") end Runtime:addEventListener("backButton", handleBackButton)
You should have complete control over your back button now. What I did so I didn’t have to use a ton of events was place the true event on exit of my main lobby scene. If the user exits that scene then it means they are in a secondary scene and I want to override the back button so when pressed they will to back to the main scene. On the show of the main scene I set the value to false and the back button will close the app. That way I only have to write 2 events in my whole app.