When I press the back button on my android device it closes the activity I'm currently on. I need to prevent this to happen. I found this question here, also I found this documentation for that event and the naming is different, then I found a third name for the same event here. I tried with all of them, even at the same time like this:
$.currentWindow.addEventListener("android:back",back);
$.currentWindow.addEventListener("androidback",back);
$.currentWindow.addEventListener("windows:back",back);
$.currentWindow.addEventListener("windowsback",back);
none of them worked, also I noticed I have to use Titanium.UI.currentWindow.addEventListener("evt", callback)
but Titanium.UI.currentWindow
seems to be Undefined. I open my window like this:
var nextWindow = core.createWindow({
controllerName : "restaurantActivity"
});
nextWindow.open();
Here is my callback function
function back(e) {
e.cancelBubble = true;
console.log(e.type);
if (Ti.App.pplatillo.length != 0) {
console.log("Confirm before exit.");
var dialog = Ti.UI.createAlertDialog({
cancel : 1,
buttonNames : ["Sí", "No"],
message : "Tienes artículos en tu carrito y el pedido no se ha concretado, si sales perderás los artículos. ¿Seguro que desea salir?",
title : "Salir"
});
dialog.addEventListener("click", function(e) {
if (e.index != e.source.cancel) {
Ti.App.pplatillo = [];
Ti.App.car = 0;
Ti.App.totalBill = 0;
$.window.close();
}
});
dialog.show();
} else {
console.log("Just exit :(");
$.window.close();
}
}
Any help will be appreciated.