简体   繁体   English

如何使用 PhoneGap 在单个 HTML 页面 webapp 中处理 Android 的后退按钮单击?

[英]How to handle Android's Back Button click in a single HTML page webapp using PhoneGap?

I read from the documentation that we can handle the back button click using the following code:我从文档中读到我们可以使用以下代码处理后退按钮单击:

document.addEventListener("backbutton", backKeyDown, true); 
function backKeyDown() { 
     // Call my back key code here.
    alert('go back!');
}

My concern is that I have a single HTML5 web page in which I have multiple div tags which I animate using jQuery as per the navigation option selected by the user from the menu options.我担心的是我有一个 HTML5 web 页面,其中我有多个div标签,我根据用户从菜单选项中选择的导航选项使用 jQuery 对其进行动画处理。

How can I, in this single page webapp, handle the back button click using PhoneGap and show the user the previously animated div .在这个单页 web 应用程序中,我如何使用 PhoneGap 处理后退按钮单击并向用户显示之前动画的div Clicking on the back button again would again take him to the previous div of the current previous div :-)再次单击后退按钮将再次将他带到当前前一个div的前一个div :-)

Thanks.谢谢。

I solved the problem by creating a global array variable as我通过创建一个全局数组变量解决了这个问题

var myStack = new Array();

Then whenever I clicked on the div tag, I inserted the function prototype along with the arguments inside the myStack variable.然后,每当我点击div标签时,我都会在myStack变量中插入 function 原型和 arguments 。 For eg:例如:

myStack.push(\"myfunction(args1, args2);\");

Then, using the code which I posted in my question, inside the BackButton handler, I wrote the following code:然后,使用我在问题中发布的代码,在BackButton处理程序中,我编写了以下代码:

var divToShow = myStack.pop();
eval(divToShow);

Hope this helps others.希望这对其他人有帮助。

I will just post my overall idea of handling this situation.我将发布我处理这种情况的总体想法。 Hope you can improvise and change it to suit your needs.希望您可以即兴创作并更改它以适合您的需求。

  1. Have a global variable to remember the current div id that is visible.有一个全局变量来记住当前可见的 div id。 For example, when a menu item x is clicked, set this global variable to the div id that is currently visible (before showing the next div corresponding to menu item x).例如,当单击菜单项 x 时,将此全局变量设置为当前可见的 div id(在显示与菜单项 x 对应的下一个 div 之前)。
  2. When the back button is pressed, use the global variable's value to identify the previous div.当按下后退按钮时,使用全局变量的值来识别前一个 div。 Hide the current div and show the previous one.隐藏当前 div 并显示前一个。

I did an implementation in a similarly structured phonegap app.我在一个类似结构的 phonegap 应用程序中做了一个实现。 My situation was a bit more complex because I was loading in html as well as external data via ajax (rather than just unhiding divs).我的情况有点复杂,因为我正在加载 html 以及通过 ajax 加载的外部数据(而不仅仅是取消隐藏 div)。 I created a global array called history which I used to keep track of current position as well as previous positions (position here being the most recent ajax function called, so the array was actually storing function names as text). I created a global array called history which I used to keep track of current position as well as previous positions (position here being the most recent ajax function called, so the array was actually storing function names as text). The right sequence and combination of.pop and.push array methods got me a fully functioning js back button that scaled nicely and handled any kind of back and forth navigation I could think of. .pop 和.push 数组方法的正确顺序和组合为我提供了一个功能齐全的 js 后退按钮,它可以很好地缩放并处理我能想到的任何类型的来回导航。

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

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