简体   繁体   English

Phonegap:导航回首页后在首页上丢失数据

[英]Phonegap : losing data on first page after navigating back to first page

Phonegap, writing app for Android. Phonegap,为Android编写应用程序。 Here is what my app supposed to do : 这是我的应用程序应该执行的操作:

1) onDeviceReady, read xml ( item names and corresponding hyperlink for each item - so kind of Table of contents) and show them on index.html 1)onDeviceReady,读取xml(每个项目的项目名称和相应的超链接-这样的目录)并在index.html上显示它们

onDeviceReady: function () {
    app.receivedEvent('deviceready');
    var readStatus = sessionStorage["readComplete"];
    alert("readComplete = " + readStatus);
    if (readStatus != "true") {
        app.readxml();
        sessionStorage["readComplete"] = "true";
    }
},


In app.readxml() I read all items + links in my datafile(XML) and add them to a ordered list in a div of Index.html page.

onDeviceReady seems to be getting called each time page loads ( like navigating back). 每次页面加载时,onDeviceReady似乎都会被调用(如导航)。 Reading same page for each page load and rebuilding seems redundant since once we read, the page should remember how it was built and controls have their old data in them.So, I store the reading xml status in sessionStorage so that i dont readxml and rebuild page each time. 对于每个页面加载读取相同的页面并进行重建似乎是多余的,因为一旦我们阅读了该页面,该页面就应该记住它是如何构建的,并且控件中有旧数据。因此,我将读取的xml状态存储在sessionStorage中,这样我就不会再读取xml并进行重建每次都翻页。

2) when user clicks on a link in the list, navigate them to a page1.xml 3) when clicks back button, bring them back to Index.html 2)当用户单击列表中的链接时,将其导航到page1.xml。3)当单击“后退”按钮时,将其带回到Index.html。

Problem at #3 --> when user comes back to first page ( index.html), at that time readStatus == true, the Index.html is showing blank. 问题#3->当用户返回首页(index.html)时,此时readStatus == true,Index.html显示为空白。

Question --> how can I maintain state of index.html ( with data populated from reading xml) so that I dont have to rebuild page on each navigation back. 问题->如何维护index.html的状态(通过读取xml填充数据),这样我就不必在每次导航时重新构建页面。

Any help is appreciated. 任何帮助表示赞赏。

Whenever you open a new link (page.html) in Cordova, the previous page (index.html) will be destroyed, this behaviour depends on the system, iOS will sometimes cache previous pages and Android seems to always clear data right away. 每当您在Cordova中打开新链接(page.html)时,上一页(index.html)都会被破坏,此行为取决于系统,iOS有时会缓存前一页,而Android似乎总是立即清除数据。

If you want to avoid reloading the XML data, you can save it into the sessionStorage or localStorage (JSON is a good format). 如果要避免重新加载XML数据,可以将其保存到sessionStorage或localStorage(JSON是一种很好的格式)。 But the DOM has to be re-built whenever you go back to index.html, there will be a minor delay still. 但是,每当您返回index.html时,都必须重新构建DOM,但仍然会有一些延迟。

A quick workaround to your problem could be creating an iframe within index.html to load page.html. 解决问题的一种快速方法是在index.html中创建一个iframe以加载page.html。 Or a floating DIV layer on top of index.html using Ajax to load page.html. 或使用Ajax加载index.html之上的浮动DIV层来加载page.html。 Both approach will work fine, assuming your index.html and page.html isn't too resource consuming. 假设您的index.html和page.html不太消耗资源,则两种方法都可以正常工作。

data-dom-cache="true" 

use this in your html tag. 在您的html标记中使用它。 for more infromation follow this... http://demos.jquerymobile.com/1.2.0/docs/pages/page-cache.html 有关更多信息,请遵循此... http://demos.jquerymobile.com/1.2.0/docs/pages/page-cache.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM