1

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.

4 Answers4

0

If you want to stop your current activity should not close on pressing the android back button. So you have to just remove/comment "$.window.close()" line from your above "back" function.

Thanks,

  • That's not the problem, my back function should close the window, but first it has to ask the user if he wants to close. The problem is that the function back never occurs because it is never called by the event, and that is because the event itself is never caught. – Guy who types fast Aug 11 '17 at 14:38
0

You can try the following approach:

$.MyControllerName.addEventListener("android:back", backCallbackFunctionHandler);

In this case whenever you press the back button, the backCallbackFunctionHandler function will be getting called. Inside that you can show an alert dialog and handle the yes/no button click to perform the required action to close or not to close the window.

Soumya
  • 1,350
  • 3
  • 19
  • 34
0

For future references, I managed solved the problem.

What I did, was add the event listener to the view in the view where I open it before opening it:

var nextWindow = core.createWindow({
    controllerName : "restaurantActivity"
});

nextWindow.addEventListener("android:back",back);

nextWindow.open();

I also had to put the callback function back in the same file.

-1

How about handling it inside the app? Using this

 @Override
public void onBackPressed() {
    super.onBackPressed();  
}
Sakura Fukuyoshi
  • 1,461
  • 1
  • 10
  • 14