简体   繁体   English

PhoneGap + jQueryMobile:Android后退按钮关闭嵌套列表中的应用程序

[英]PhoneGap + jQueryMobile: Android back button closes app in nested list

I'm creating an app using PhoneGap and jQuery Mobile. 我正在使用PhoneGap和jQuery Mobile创建一个应用程序。

Using jQuery Mobile I have created a nested list. 使用jQuery Mobile我创建了一个嵌套列表。

After clicking into the nested list I want to go back. 点击进入嵌套列表后我想回去。 I expect that clicking the back button on my Android device ( Nokia N1 ) that it will go back one level. 我希望单击我的Android设备( 诺基亚N1 )上的后退按钮,它将返回一个级别。

But instead android closes the app instead of going back up one level. 但相反,Android关闭应用程序而不是回到一个级别。

I am using PhoneGap 1.2.0, jQuery Mobile v1.0rc2, jQuery 1.6.4, and Android 2.3.3 (Gingerbread). 我正在使用PhoneGap 1.2.0,jQuery Mobile v1.0rc2,jQuery 1.6.4和Android 2.3.3(Gingerbread)。

I also upgraded to jQuery Mobile 1.0, and there is no change. 我也升级到jQuery Mobile 1.0,没有任何变化。

You can listen for back button events: 您可以收听后退按钮事件:

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown() {
    // Handle the back button
}

And if the current page is the homepage ( $.mobile.activePage in jQuery Mobile) exit from application with: 如果当前页面是主页(jQuery Mobile中的$.mobile.activePage )退出应用程序,则:

navigator.app.exitApp();

I've got this same problem. 我有同样的问题。 I found out how to handle the back button in the Java code. 我发现了如何处理Java代码中的后退按钮。

This goes back one step if possible or else exits the app. 如果可能,这会返回一步,否则退出应用程序。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
    if(appView.canGoBack()){
       appView.goBack();
        return true;
    }
  }
  return super.onKeyDown(keyCode, event);
}

It is also possible to do it on the JavaScript side: 也可以在JavaScript端进行:

document.addEventListener("backbutton", function() {
    //Logic//
}, false);

Also try: 还试试:

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

    if ($.mobile.activePage.attr('id') == 'main') {
         <!-- navigator.app.exitApp(); -->
         device.exitApp();
    }
    else {
         history.back(1);
    } 
}

For those who came here because the app closes after having been in focused text-filed and pressing the backButton: 对于那些来到这里的人,因为应用程序在关注文本字段并按下backButton之后关闭:

In Android 4.0.3 (ICS) you do not have to override backButton with the PhoneGap API to stop closing/crashing the app after having focused an input text-field and then pressing the back button. 在Android 4.0.3(ICS)中,您不必使用PhoneGap API覆盖backButton,以便在聚焦输入文本字段然后按后退按钮后停止关闭/崩溃应用程序。 Normally this closes the app, because WebKit is creating a tap-highlight with an additional outline that can not be changed with CSS. 通常这会关闭应用程序,因为WebKit正在创建一个带有附加轮廓的点按高亮显示,无法使用CSS更改。

When you focus the input the softkeyboard comes up. 当您聚焦输入时,软键盘会出现。 When you press the first time the backButton your softkeyboard disappears. 当您第一次按下backButton时,您的软键盘会消失。 When you click again to go back in navigation-history, the app is closing instead of jumping to the page visited before. 当您再次单击以返回导航历史记录时,应用程序正在关闭,而不是跳转到之前访问过的页面。 This is because the highlighting jumps out of the navigation-structure. 这是因为突出显示跳出了导航结构。 It seems like it is not in the DOM. 好像它不在DOM中。 I don't really understand that behaviour. 我真的不明白这种行为。 Here is the solution: 这是解决方案:

Just add 只需添加

input {
-webkit-user-modify: read-write-plaintext-only
}

This interrupts webkit doing a tap-highlight and you still stay in the app and can go back in History with your (not overriden) backButton. 这会中断webkit进行点击突出显示,你仍然可以留在应用程序中,并可以使用你的(不是覆盖)backButton返回历史记录。

I came here because of the issue with the back button inside a text field ( input or textarea ) in a Cordova/PhoneGap app that doesn't trigger the normal behavior (in my case, it doesn't trigger my JavaScript handler). 我来到这里是因为Cordova / PhoneGap应用程序中的文本字段( inputtextarea )中的后退按钮问题不会触发正常行为(在我的情况下,它不会触发我的JavaScript处理程序)。

Unfortunately, the above solution using CSS doesn't work on Android 2.3. 不幸的是,使用CSS的上述解决方案在Android 2.3上不起作用。

And the chosen solution, which overrides the event in Java, isn't enough for me because I need to trigger a JavaScript handler, not go back in the webview. 所选择的解决方案覆盖了Java中的事件,对我来说还不够,因为我需要触发一个JavaScript处理程序,而不是回到webview中。 Also, this solution is reimplementing the default Cordova behaviour, which is not best practice, as it loses other built-in features. 此外,此解决方案正在重新实现默认的Cordova行为,这不是最佳做法,因为它会丢失其他内置功能。

So, what I did (and it worked) was override the KeyUp event, as above, but instead of reimplementing it, I just called the handler on the appVIew (which is Cordova's implementation). 所以,我所做的(并且它起作用)覆盖了KeyUp事件,如上所述,但是我没有重新实现它,而是在appVIew上调用处理程序(这是Cordova的实现)。

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    return appView.onKeyUp(keyCode, event);
}

This is the problem of using things like PhoneGap. 这是使用PhoneGap之类的问题。 They build applications the way that Android doesn't work. 他们以Android无法运行的方式构建应用程序。

The back button pops the latest activity of the activity stack. 后退按钮弹出活动堆栈的最新活动。 As you have a whole application that looks like multiple activities, it's in fact a single activity with overlays. 由于您有一个看起来像多个活动的整个应用程序,它实际上是一个带有叠加层的活动。 What you will have to do, and keep in mind this, this isn't really the proper way of doing things. 你将要做的事情,并牢记这一点,这不是真正的做事方式。

You will have to override the functionality of the back button pragmatically to get it to go back in the jQuery stacks, and then once the flat Android application is the only thing that's left, go back from that too. 您将不得不实际地覆盖后退按钮的功能以使其返回到jQuery堆栈中,然后一旦平面Android应用程序是唯一剩下的东西,也可以从那里返回。 I don't know if PhoneGap allows you to have that much control over the Android system, but by default, the Android OS won't act normally with your jQuery list. 我不知道PhoneGap是否允许你对Android系统有那么多的控制权,但默认情况下,Android操作系统不能正常使用你的jQuery列表。

I hope this provides some insight. 我希望这能提供一些见解。

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

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