Titanium 钛:陷阱安卓后退按钮

Titanium 钛:陷阱安卓后退按钮,titanium,Titanium,我想在退出应用程序之前捕获android后退按钮以强制确认。我的应用程序是一个两窗口/两个选项卡的应用程序,基于titanium cli创建的框架。我已经尝试了以下三种方法。据我所知,后退按钮从未被卡住,当按下后退按钮时,应用程序就会关闭 钛合金3.2.0 CLI OSX10.9 // below true of all windows: // modal: true, // exitOnClose:true // android:back and androidback hav

我想在退出应用程序之前捕获android后退按钮以强制确认。我的应用程序是一个两窗口/两个选项卡的应用程序,基于titanium cli创建的框架。我已经尝试了以下三种方法。据我所知,后退按钮从未被卡住,当按下后退按钮时,应用程序就会关闭

钛合金3.2.0 CLI OSX10.9

  // below true of all windows:
  // modal: true,
  // exitOnClose:true
  // android:back and androidback have been tried

Ti.App.addEventListener('androidback', function(e){
    var confirmClear = Titanium.UI.createAlertDialog({


        message:'Exit App?', 
        buttonNames: ['Yes','No']
    });
    confirmClear.show();
    confirmClear.addEventListener('click',function(e) {
        if (e.index === 0) {

         win1.close();

          }
    });
});

//////////////////////////////////////////////////  

win1.addEventListener('androidback', function(e){
    var confirmClear = Titanium.UI.createAlertDialog({


        message:'Exit App?', 
        buttonNames: ['Yes','No']
    });
    confirmClear.show();
    confirmClear.addEventListener('click',function(e) {
        if (e.index === 0) {

         win1.close();

          }
    });
});

//////////////////////////////////////////////////  

win2.addEventListener('androidback', function(e){
    var confirmClear = Titanium.UI.createAlertDialog({

        message:'Exit App?', 
        buttonNames: ['Yes','No']
    });

    confirmClear.show();

    confirmClear.addEventListener('click',function(e) {
        if (e.index === 0) {

         win2.close();

          }
    });
});
在阅读了这些建议之后,以下是我现在的工作代码:

tabGroup.addEventListener('androidback', function(e){
    var confirmClear = Titanium.UI.createAlertDialog({


        message:'Exit App?', 
        buttonNames: ['Yes','No']
    });
    confirmClear.show();
    confirmClear.addEventListener('click',function(e) {
        if (e.index === 0) {

         tabGroup.close();


          }
    });
});

您应该仅对win1使用exitOnClose事件


还告诉我你用的是哪一种?

首先,我总是用
android:back

当您使用tabGroups方法时,您应该知道这些选项卡链接到软窗口(
win1
win2
),也许这就是为什么它们没有捕捉到
android:back
事件的原因。 也就是说,您应该在tabGroup(ApplicationTabGroup.js)中捕获此事件。在这里,您应该添加如下内容:

function ApplicationTabGroup(Window) {
    //create module instance
    var self = Ti.UI.createTabGroup();
...

    self.addEventListener('android:back', function(e){
        var confirmClear = Titanium.UI.createAlertDialog({
            message:'Exit App?', 
            buttonNames: ['Yes','No']
        });
        confirmClear.show();
        confirmClear.addEventListener('click',function(e) {
            if (e.index === 0) {
                win1.close(); //this line won't work
                //$.index.activeTab = 1; //use this to switch to tab2

            }
        });
    });
    return self;    
}
另一件事,如果您试图切换到另一个选项卡,您应该尝试
self.activeTab=1而不是
win1.close()
win1.close()是选项卡的一部分(
win1.containingTab=tab1;
)。如果要删除该选项卡,在Android中,方法
removeTab
也不会起作用。

尝试使用“Android:back”来代替。

在代码中的注释中://Android:back和androidback已经试用了安腾3.2.0 CLI。仅为win1添加侦听器不起作用。我恢复到Tianium 3.1.4,只是想看看3.2更新是否破坏了某些功能。没有快乐,还是一样的问题。