Android设备后退按钮在sencha touch应用程序中工作

Android设备后退按钮在sencha touch应用程序中工作,android,sencha-touch,back-button,Android,Sencha Touch,Back Button,我已经在我的应用程序中使用sencha touch历史记录和路由实现了后退按钮支持。但只有当应用程序通过浏览器运行时,它才能工作。当我将应用程序打包为本机应用程序时,按下后退按钮即可关闭应用程序。 如何在原生android打包应用程序中处理设备后退按钮?除了sencha touch routing和history支持之外,我还需要任何其他实现,以便在通过浏览器和打包的android应用程序运行时,back按钮的工作方式相同吗 我的控制器: Ext.define('oscommercecatalo

我已经在我的应用程序中使用sencha touch历史记录和路由实现了后退按钮支持。但只有当应用程序通过浏览器运行时,它才能工作。当我将应用程序打包为本机应用程序时,按下后退按钮即可关闭应用程序。 如何在原生android打包应用程序中处理设备后退按钮?除了sencha touch routing和history支持之外,我还需要任何其他实现,以便在通过浏览器和打包的android应用程序运行时,back按钮的工作方式相同吗

我的控制器:

Ext.define('oscommercecatalog.controller.Category', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            main: 'main'
        },

        before: {
            showRootCategory: 'ensureStoreLoad',
            showCategoryById: 'ensureStoreLoad'
        },

        control: {
            main: {
                beforepop: 'onMainBeforePop'
            },
            categories: {
                itemtap: 'onCategoryTap'
            },
            productslist: {
                itemtap: 'onProductTap'
            }
        },

        routes: {
            '': 'showRootCategory',
            ':id': 'showCategoryById'
        },

        currentRecord: null,

        stack: []
    },

    init: function() {
        Ext.getStore('Categories').on('load', this.onStoreLoad, this);
    },

    ensureStoreLoad: function(action) {
        var store = Ext.getStore('Categories');

        if (store.data.all.length) {
            action.resume();
        } else {
            store.on('load', function() {
                action.resume();
            }, this, {
                single: true
            });
        }
    },

    onMainBeforePop: function() {
        var history = this.getApplication().getHistory(),
            record = this.getCurrentRecord().parentNode,
            urlId = (record && record.get('urlId')) ? record.get('urlId') : '',
            productView = this.productView,
            stack = this.getStack();

        this.setCurrentRecord(record);

        history.add(new Ext.app.Action({
            url: urlId
        }), true);

        stack.pop();
        this.setStack(stack);

        if (productView && !productView.isHidden()) {
            productView.hide();
        }
    },

    showRootCategory: function() {
        var stack = this.getStack();

        if (stack.length) {
            this.getMain().pop();
            return;
        }

        this.setStack([]);

        var store = Ext.getStore('Categories'),
            record = store.getRoot();

        this.addPreviousViews(record);
        this.showCategory(record);
    },

    onCategoryTap: function(view, index, target, record, e) {
        console.log('Category');
        this.redirectTo(record);
    },

    showCategoryById: function(id) {
        var store = Ext.getStore('Categories'),
            stack = this.getStack(),
            previousStackItem = stack[stack.length - 2],
            records, record;

        if (previousStackItem && previousStackItem == id) {
            this.getMain().pop();
            return;
        }

        records = Ext.Array.filter(store.data.all, function(record) {
            if (record.get('urlId') == id) {
                return record;
            }
        }, this);

        record = records[0];
        if (record) {
            this.addPreviousViews(record);

            stack.push(id);
            this.setStack(stack);

            if (record.childNodes.length) {
                this.showCategory(record);
            } else {
                this.showProducts(record);
            }
        } else {
            Ext.Logger.warn('Category not found');
        }
    },

    addPreviousViews: function(record) {
        var parents = [],
            main = this.getMain(),
            layout = main.getLayout(),
            animation = layout.getAnimation(),
            stack = this.getStack(),
            ln, i, urlId;

        if (main.getInnerItems().length) {
            return;
        }

        while ((record = record.parentNode)) {
            parents.unshift(record);
        }

        layout.setAnimation(false);

        ln = parents.length;
        for (i = 0; i < ln; i++) {
            urlId = parents[i].get('urlId');
            if (urlId) {
                stack.push(urlId);
            }

            this.showCategory(parents[i]);
        }

        this.setStack(stack);

        setTimeout(function() {
            layout.setAnimation(animation);
        }, 50);
    },

    showCategory: function(record) {
        var isRoot = (record.get('id') == "root"),
            view;

        if (isRoot) {
            record.set('label', 'Categories');
        }

        this.setCurrentRecord(record);

        view = this.getCategoriesView({
            title: record.get('label'),
            cls: (isRoot) ? 'root' : null,
            data: record.childNodes
        });

        this.getMain().setActiveItem(view);
    },

    showProducts: function(record) {
        this.setCurrentRecord(record);

        var store = record.products();
        store.getProxy().setExtraParam('cat', record.get('urlId'));
        store.load();

        //empty the store before adding the new one
        var productsStore = this.productsView.getStore();
        if (productsStore) {
            productsStore.removeAll();
        }

        this.productsView.setStore(store);

        this.getMain().setActiveItem(this.productsView);
    },

    /**
     * Called when an item is tapped on.
     * This is overridden in the Tablet controller
     */
    onProductTap: function(view, record) {
        var productView = this.getProductView();

        productView.setData(record.data);

        if (!productView.getParent()) {
            Ext.Viewport.add(productView);
        }

        productView.show();
    },

    /**
     * This creates and returns a new categories view, for when it is needed.
     * Ideally this should be improved at some point to only instansiate a max of 2 views
     * and then reuse the same views over again.
     * @param {Object} config The configuration for the view.
     * @return {Catalog.view.Categories} view
     */
    getCategoriesView: function(config) {
        return Ext.create('oscommercecatalog.view.Categories', config);
    },

    /**
     * This function is used to create and return a product view.
     * There is a different products view for both phone and tablet profiles, so we just have an emptyFn
     * in this base controller, and in the tablet/phone controller we will override this.
     * @param {Object} config The configuration for the view.
     * @return {Ext.Component} view
     */
    getProductsView: Ext.emptyFn,

    /**
     * This function is used to create and return a the product view.
     * There is a different product view for both phone and tablet profiles, so we just have an emptyFn
     * in this base controller, and in the tablet/phone controller we will override this.
     * @param {Object} config The configuration for the view.
     * @return {Ext.Component} view
     */
    getProductView: Ext.emptyFn
});
Ext.define('oscommercecatalog.controller.Category'{
扩展:“Ext.app.Controller”,
配置:{
参考文献:{
main:“main”
},
之前:{
showRootCategory:“ensureStoreLoad”,
showCategoryById:“ensureStoreLoad”
},
控制:{
主要内容:{
beforepop:“onMainBeforePop”
},
类别:{
itemtap:“onCategoryTap”
},
产品列表:{
itemtap:“onProductTap”
}
},
路线:{
'''showRootCategory',
“:id”:“showCategoryById”
},
currentRecord:null,
堆栈:[]
},
init:function(){
Ext.getStore('Categories')。on('load',this.onStoreLoad,this);
},
确保恢复负载:功能(操作){
var store=Ext.getStore('Categories');
if(store.data.all.length){
动作。恢复();
}否则{
store.on('load',function()){
动作。恢复();
}这,{
单身:对
});
}
},
onMainBeforePop:function(){
var history=this.getApplication().getHistory(),
record=此.getCurrentRecord().parentNode,
urlId=(record&&record.get('urlId'))?record.get('urlId'):“”,
productView=this.productView,
stack=this.getStack();
此.setCurrentRecord(记录);
添加(新的Ext.app.Action)({
url:urlId
}),对);
stack.pop();
这是固定钉(堆叠);
if(productView&!productView.ishiden()){
productView.hide();
}
},
showRootCategory:函数(){
var stack=this.getStack();
if(堆栈长度){
this.getMain().pop();
回来
}
这个.setStack([]);
var store=Ext.getStore('Categories'),
record=store.getRoot();
此.addPreviousViews(记录);
本.展示类别(记录);
},
onCategoryTap:函数(视图、索引、目标、记录、e){
console.log('Category');
这个。重定向到(记录);
},
showCategoryById:函数(id){
var store=Ext.getStore('Categories'),
stack=this.getStack(),
previousStackItem=堆栈[stack.length-2],
记录,记录,;
if(previousStackItem&&previousStackItem==id){
this.getMain().pop();
回来
}
记录=Ext.Array.filter(store.data.all,函数(记录){
if(record.get('urlId')==id){
返回记录;
}
},这个);
记录=记录[0];
如果(记录){
此.addPreviousViews(记录);
堆栈推送(id);
这是固定钉(堆叠);
if(record.childNodes.length){
本.展示类别(记录);
}否则{
本次展示产品(记录);
}
}否则{
Ext.Logger.warn('未找到类别');
}
},
addPreviousViews:函数(记录){
var parents=[],
main=this.getMain(),
layout=main.getLayout(),
animation=layout.getAnimation(),
stack=this.getStack(),
ln,i,urlId;
if(main.getInnerItems().length){
回来
}
while((record=record.parentNode)){
父母。解除移位(记录);
}
布局。设置动画(错误);
ln=双亲长度;
对于(i=0;i@Override
public void onBackPressed() {
    // your code.
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
    }

    return super.onKeyDown(keyCode, event);
}
if (Ext.os.is('Android')) {
    document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);

    function onBackKeyDown(eve) {   
        eve.preventDefault();

        //do something
        alert('back button pressed');

    }
}