Javascript History.pushState和穿越历史-PhoneGap/Android

Javascript History.pushState和穿越历史-PhoneGap/Android,javascript,android,cordova,cordova-2.0.0,html5-history,Javascript,Android,Cordova,Cordova 2.0.0,Html5 History,我正在为一个客户端编写一个Android应用程序。我一直在使用我自己的框架来创建应用程序 这个框架在iPhone上运行没有问题,但当我将它应用到Android/Phonegap时,我发现后退按钮有问题。我的解决方案是调用History对象,在查看不同页面时创建项目。本守则的要点如下: 'hook_call': function (name, data, callback, skip_history) { if (typeof callback != 'f

我正在为一个客户端编写一个Android应用程序。我一直在使用我自己的框架来创建应用程序

这个框架在iPhone上运行没有问题,但当我将它应用到Android/Phonegap时,我发现后退按钮有问题。我的解决方案是调用History对象,在查看不同页面时创建项目。本守则的要点如下:

        'hook_call': function (name, data, callback, skip_history) {

            if (typeof callback != 'function') {
                callback = function () {};
            }

            app.handlers[name].call(this, data, callback);
            window.app.current_hook = name;

            // Manage history
            if(!skip_history ) {
                history.pushState({'page': name, 'data': data}, null, '#'+name);
            }

        }
我的onpopstate函数:

// Manage history
window.onpopstate = function(event) {
    try {
        if(window.app.current_hook != event.state.page)
            window.app.hook_call(event.state.page, event.state.data);
    }
    catch(e) {}
};
因此,这会正确地添加项目。但是,如果我在历史中说三个级别(
home
listings
->
view\u listing
),然后从
view\u listing
按back键,我将被带到
listings
部分,这是正确的,但再按一下,我就只能在
listings
,它不会再往后移动了

当我在刚才解释的导航步骤之后查看日志时,我看到了这些虚假的项目:

04-16 10:22:39.320: D/CordovaWebView(1148): The current URL is: file:///android_asset/www/index.html#listing
04-16 10:22:39.320: D/CordovaWebView(1148): The URL at item 0 is:file:///android_asset/www/index.html
04-16 10:22:39.400: D/CordovaWebView(1148): The URL at index: 0is file:///android_asset/www/index.html
04-16 10:22:39.400: D/CordovaWebView(1148): The URL at index: 1is file:///android_asset/www/index.html#welcome
04-16 10:22:39.400: D/CordovaWebView(1148): The URL at index: 2is file:///android_asset/www/index.html#listings
04-16 10:22:39.410: D/CordovaWebView(1148): The URL at index: 3is file:///android_asset/www/index.html#listing
我应该补充一下,为了进入不同的页面,例如当点击列表或按下按钮时,会调用
hook\u call()

此场景适用于通过应用程序进行导航的任何其他组合


有人有过类似的经验吗?

我相信您的函数应该是这样的,设置了skip_history标志:

// Manage history
window.onpopstate = function(event) {
    try {
        if(window.app.current_hook != event.state.page)
            window.app.hook_call(event.state.page, event.state.data, null, true);
    }
    catch(e) {}
};

由于您不想将历史记录推送到堆栈上,因此只需将其弹回到堆栈上。

我认为,设置了skip_history标志后,您的函数应该是这样的:

// Manage history
window.onpopstate = function(event) {
    try {
        if(window.app.current_hook != event.state.page)
            window.app.hook_call(event.state.page, event.state.data, null, true);
    }
    catch(e) {}
};

因为您不想推送历史记录,所以只会弹出到堆栈上。

您使用的是哪个版本的android和phonegap?我使用的是Cordova 2.4.0,这应该适用于任何受支持的android版本。您测试的android版本是什么?蜂巢中使用loadUrl(Cordova使用)和散列时出现了一个bug:也许这与此有关?最新的Cordova(2.6)是否仍存在此问题?有什么特别的原因让你编写自己的历史模块而不是使用Cordova内置的模块吗?嗨,Billau先生,谢谢你的回复。我使用的模拟器运行的是API级别为17的4.2。我将查看Cordova的最新版本,看看这是否有任何帮助。然而,我并没有编写自己的历史模块,只是使用原生HTML5作为我的应用程序,只运行一个标准的HTML页面。你能发布你的window.onpopstate函数吗?你使用的是哪个版本的android和phonegap?我使用的是Cordova 2.4.0,这应该适用于任何受支持的android版本。你测试过哪个版本的android?蜂巢中使用loadUrl(Cordova使用)和散列时出现了一个bug:也许这与此有关?最新的Cordova(2.6)是否仍存在此问题?有什么特别的原因让你编写自己的历史模块而不是使用Cordova内置的模块吗?嗨,Billau先生,谢谢你的回复。我使用的模拟器运行的是API级别为17的4.2。我将查看Cordova的最新版本,看看这是否有任何帮助。然而,我并没有编写自己的历史模块,只是使用原生HTML5作为我的应用程序,它只运行一个标准HTML页面。你能发布你的window.onpopstate函数吗?