钛合金后退按钮-总是关闭我的应用程序[英] Titanium Back Button - Always closes my App

本文是小编为大家收集整理的关于钛合金后退按钮-总是关闭我的应用程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的应用程序有一个新问题,我正在使用appcelerator钛进行开发. 我想从设备上锁定backutton,因此,当我使用他时,应用程序不会关闭.我只想在我在PrimaryMenu时关闭该应用程序.所以这是我的代码:

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;
});

我有一个变量,我可以从菜单中从Hierachy中跟踪用户的位置.因此,只有在位置为" 0".

时,该应用才应关闭

如果位置为" 1",则应该有一个动画,因此可以正常工作,但是在动画期间,应用程序立即关闭.

窗口的代码是:

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]
});

推荐答案

您要监视的事件android:back已弃用.请使用androidback事件.

第二步是停止在事件处理程序链中进一步冒泡事件.为此,您需要取消活动:

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

您还必须修改窗口并将exitOnClose属性设置为false

本文地址:https://www.itbaoku.cn/post/1938180.html

问题描述

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]
});

推荐答案

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