Press "Enter" to skip to content

使用framework7框架开发Android退出键不能退出的问题

近日开始学习开发App,使用framework7框架开发,还是比较简单,但是也有一些坑,比如默认的模板在Android上退出键是无效。

刚接触,无从下手,在论坛上提问也没有人回答,于是到处搜索解决方法,最后发现在capacitor-app.js文件上有一段是控制后退键的代码,只要加上一段小代码即可,最后代码如下。

handleAndroidBackButton: function () {
    var f7 = capacitorApp.f7;
    const $ = f7.$;
    if (f7.device.electron || !window.Capacitor) return;
    window.Capacitor.Plugins.App.addListener('backButton', function () {
      if ($('.actions-modal.modal-in').length) {
        f7.actions.close('.actions-modal.modal-in');
        return;
      }
      if ($('.dialog.modal-in').length) {
        f7.dialog.close('.dialog.modal-in');
        return;
      }
      if ($('.sheet-modal.modal-in').length) {
        f7.sheet.close('.sheet-modal.modal-in');
        return;
      }
      if ($('.popover.modal-in').length) {
        f7.popover.close('.popover.modal-in');
        return;
      }
      if ($('.popup.modal-in').length) {
        if ($('.popup.modal-in>.view').length) {
          const currentView = f7.views.get('.popup.modal-in>.view');
          if (currentView && currentView.router && currentView.router.history.length > 1) {
            currentView.router.back();
            return;
          }
        }
        f7.popup.close('.popup.modal-in');
        return;
      }
      if ($('.login-screen.modal-in').length) {
        f7.loginScreen.close('.login-screen.modal-in');
        return;
      }

      if ($('.page-current .searchbar-enabled').length) {
        f7.searchbar.disable('.page-current .searchbar-enabled');
        return;
      }

      if ($('.page-current .card-expandable.card-opened').length) {
        f7.card.close('.page-current .card-expandable.card-opened');
        return;
      }

      const currentView = f7.views.current;
      if (currentView && currentView.router && currentView.router.history.length > 1) {
        currentView.router.back();
        return;
      }

      if ($('.panel.panel-in').length) {
        f7.panel.close('.panel.panel-in');
        return;
      }


        if (window.navigator.app && window.navigator.app.exitApp) {
          f7.dialog.confirm('Close Application?', function () {
 
              window.navigator.app.exitApp();
     
          });
        }
        return true;


    }, false);
  },