在Sencha touch中,如果我使用导航视图,我可以返回按钮。这很好。
但是如果用户点击设备后退怎么办?它是直接退出应用程序。在我的要求中,它不应该退出它必须返回到前一个屏幕的应用程序。我怎么能这样做?。
答案 0 :(得分:8)
你可以像这样处理硬件后退按钮:
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
答案 1 :(得分:6)
我没有在历史支持页面上找到有用的说明;在处理导航视图时,无论如何都无法看到使用路线,导航视图可以随时拥有大量的视图。
如果您只想使用后退按钮,则可以使用popstate
和pushstate
功能(请参阅https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history以获取参考)。我们的想法是,您在添加视图时push
处于状态,并在删除视图时将其pop
关闭。 Android手机上的物理后退按钮或桌面浏览器上的后退按钮可有效调用history.back()
;所以你需要做的就是确保按下标题栏上的后退按钮做同样的事情,这就是触发导航视图弹出的那个。
为了使用它在Sencha Touch中工作,我将以下内容添加到主控制器:
在refs中,我引用了main
视图(Ext.navigation.View
的一个实例)及其标题栏,您可以从中引用后退按钮的事件,例如:
refs: {
main: 'mainview',
mainTitleBar: 'mainview titlebar',
}...
我通过control
配置对象附加以下功能..
control: {
main: {
push: 'onMainPush'
},
mainTitleBar: {
back: 'onBack'
},
...
这些定义为:
onMainPush: function(view, item) {
//do what ever logic you need then..
history.pushState();
},
onBack: function() {
history.back(); //will cause the onpopstate event to fire on window..
//prevent back button popping main view directly..
return false;
},
然后我附加一个函数,当通过..的函数弹出状态时执行。
init: function() {
/* pop a view when the back button is pressed
note: if it's already at the root it's a noop */
var that = this;
window.addEventListener('popstate', function() {
that.getMain().pop();
}, false);
},
现在,按回标题栏,执行history.back()
,这会依次触发popstate
事件,然后导致主视图弹出。
如果你想在真正的应用程序上看到这个,那么在github here上有一个(v。basic!)属性查找器应用程序使用这种技术。