4

I have a new problem with my App, i'm using Appcelerator Titanium for development. I want to lock the Backbutton from my Device, so the App wont close everytime when i use him. I want to close the App only if i'm at the primarymenu. So this is my code:

Ti.UI.currentWindow.addEventListener('android:back',function(){
alert(Ti.App.PositionNow);
if(Ti.App.PositionNow=='0') {
    alert('do quit');
} else if(Ti.App.PositionNow=='1') {
    Ti.App.multi_tableView.addEventListener('click',myfunction);
    var duration = 300;
    var setOldOpacity = Titanium.UI.createAnimation();
    setOldOpacity.opacity = 1;
    setOldOpacity.zIndex = 1;
    setOldOpacity.duration = duration;

    var setOldBottom = Titanium.UI.createAnimation();
    setOldBottom.bottom = 0;
    setOldBottom.duration = duration;

    var setOldTop = Titanium.UI.createAnimation();
    setOldTop.top = 0;
    setOldTop.duration = duration;

    var animationHandler2 = function() {
           setOldTop.removeEventListener('complete',animationHandler2);
           Ti.App.multi_view_first.animate(setOldTop);
           Ti.App.multi_view_second.animate(setOldBottom);
           Ti.App.multi_tableView.animate(setOldOpacity);
        };
    setOldTop.addEventListener('complete',animationHandler2);
    Ti.App.multi_view_first.animate(setOldTop);
    Ti.App.multi_view_second.animate(setOldBottom);
    Ti.App.multi_tableView.animate(setOldOpacity);
    alert('hallo1');
    Ti.App.PositionNow = 0;
}
return false;
});

I have an variable where i track the position from the user at the hierachy from the menu. So the App should only close when the position is "0".

If the position is "1", there should be an animation, so this works, but during the animation, the app closes instantly.

The code of the window is this:

Ti.App.hs_win = Ti.UI.createWindow({
   url: '/sites/homescreen/index.js',
   navBarHidden: true,
   fullscreen: true,
   modal:true,
   theme: "Theme.Titanium",
   orientationModes: [Ti.UI.PORTRAIT]
});
  • Check out this post on the appcelerator forum:https://developer.appcelerator.com/question/159538/android-back-button. for basic back navigation you dont need to overide the back event. they suggest using exitOnClose="true" – Jeroen Jan 20 '15 at 08:16
  • so this works, the window doesn't close when i press the back button, but there is now another problem, my "back animation" isn't working now. it seems like the window is cleared complete when i press the back button. – Booster4App Jan 20 '15 at 18:18
  • okay, that was an part of the answer ! thank you very much for your time! – Booster4App Jan 20 '15 at 22:59

1 Answers1

5

The event you're monitoring android:back is deprecated. Please use androidback event.

The second step is to stop the event bubbling further in the event handler chain. For this you need to cancel the event:

Ti.UI.currentWindow.addEventListener('androidback',function(event){
      event.cancelBubble = true;
}

Also you have to modify your window and set exitOnClose property to false

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
  • that doesn't work. If i try your example, the app closes instantly, without doing the animation or anything else. – Booster4App Jan 20 '15 at 18:33
  • okay, now it works like the other one. i can just see the splashscreen when i press the back button ... – Booster4App Jan 20 '15 at 22:35
  • okay, now it works, i saw that i have in another file (i've put every window in a single file) the same eventlistener on Ti.UI.currentWindow ... i've deleted the second one, now it works ... got damn ... it's not the same as javascript, just most of the time, but not everything is same... ;) – Booster4App Jan 20 '15 at 22:51