Ionic 电容器硬件返回按钮关闭发布版本中的应用程序

rqcrx0a6  于 2022-12-09  发布在  Ionic
关注(0)|答案(2)|浏览(147)

我通过在react项目的_app.tsx中添加一个侦听器来处理后退按钮,如下所示

useEffect(() => {
        CapacitorApp.addListener("backButton", () => {
            if (router.pathname === "/") {
                CapacitorApp.exitApp();
            } else {
                router.back();
            }
        });

        return () => {
            CapacitorApp.removeAllListeners();
        };
    }, [router]);

并且它在开发版本中按预期工作,没有任何问题。
但在发布版本中,后退按钮将关闭应用程序
我的电容器封装版本

"@capacitor/android": "^3.5.1",
"@capacitor/app": "^1.1.1",
"@capacitor/cli": "^3.5.1",
"@capacitor/core": "^3.5.1",

我已经尝试删除整个android文件夹和readding它
而且问题只发生在AAB(Android应用程序捆绑包),如果我建立一个正常的APK,后退按钮逻辑工作完美,不退出应用程序。

ldioqlga

ldioqlga1#

这是一个已知问题,您只需将以下proguard规则复制到应用的proguard规则中

# Rules for Capacitor v3 plugins and annotations
 -keep @com.getcapacitor.annotation.CapacitorPlugin public class * {
     @com.getcapacitor.annotation.PermissionCallback <methods>;
     @com.getcapacitor.annotation.ActivityCallback <methods>;
     @com.getcapacitor.annotation.Permission <methods>;
     @com.getcapacitor.PluginMethod public <methods>;
 }

# Rules for Capacitor v2 plugins and annotations
# These are deprecated but can still be used with Capacitor for now
-keep @com.getcapacitor.NativePlugin public class * {
  @com.getcapacitor.PluginMethod public <methods>;
}

# Rules for Cordova plugins
-keep public class * extends org.apache.cordova.* {
  public <methods>;
  public <fields>;
}

您可以在www.example.com上查看此问题和解决方法https://github.com/ionic-team/capacitor/issues/5606#issuecomment-1122461215

pwuypxnk

pwuypxnk2#

我认为你订阅错误。它应该是'ionBackButton'。就像它在文档中说的那样(在这里):

ev.detail.register(10, () => {
    console.log('Handler was called!');
  });
});

相关问题