简体   繁体   English

Phonegap Android后退按钮覆盖

[英]Phonegap Android back button override

In my Phonegap app, created in a single html file using div as pages, I override the Back button to exit the app, only if the div that acts as the Home page is visible, otherwise to hide the others and show the home div . 在我的Phonegap应用程序中(使用div作为页面在单个html文件中创建),我覆盖了“后退”按钮以退出应用程序,只有当充当主页的div可见时,否则隐藏其他按钮并显示home div I use jQuery to attach the event handlers. 我使用jQuery附加事件处理程序。

It works well at first app launch, but if the app is in the History list, the override is not working, the Back button exits the app without checking which div is visible. 它在首次启动应用程序时运行良好,但是如果该应用程序在“历史记录”列表中,则替代功能不起作用,“后退”按钮将退出该应用程序,而不检查哪个div可见。 After deleting the app from the History list it works as expected again. 从“历史记录”列表中删除该应用程序后,它将再次按预期运行。

Tested on Nexus 4 with Android 4.2. 在Nexus 4和Android 4.2上进行了测试。 Here is the code: 这是代码:

    $(document).on('backbutton', function (ev) {
        ev.preventDefault();
        if (!$('#divHomeScreen').is(':visible')) {
            $('.screen').hide();
            $('#divHomeScreen').show();
            return false;
        } else {
            navigator.app.exitApp();
        }
    });

Thanks for your help. 谢谢你的帮助。

What I have done is dynamically add and remove the backbutton handler as needed. 我要做的是根据需要动态添加和删除backbutton处理程序。 For example... 例如...

    function showScreen()
    {
      $("#divHomeScreen").hide();
      $(".screen").show();
      $(document).on("backbutton", onBackButton);
    }

    function hideScreen()
    {
      $(".screen").hide();
      $("#divHomeScreen").show();
      $(document).off("backbutton", onBackButton);
    }

    function onBackButton()
    {
      hideScreen();
    }

This was tested on Galaxy S3 Android 4.3 and PhoneGap 3.3.0 已在Galaxy S3 Android 4.3和PhoneGap 3.3.0上进行过测试

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

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