silentnow
silentnow

Reputation: 643

Capacitor Hard ware back button closing the app in release build

I am handling the back button by adding a listener in the _app.tsx of my react project like so

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

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

and it is working as expected in the dev build without any issues.

But in the release build the back button is closing the app

My capacitor package versions

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

I have tried removing the whole android folder and readding it

and also issue only happening with aab(android app bundle) if I build a normal apk the back button logic is working perfectly and not exiting the app.

Upvotes: 1

Views: 1275

Answers (2)

Nikhil Reddy Avuthu
Nikhil Reddy Avuthu

Reputation: 68

It is a known issue you just need to copy the following proguard rules into your app's proguard rules

# 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>;
}

You can see the issue and workaround at https://github.com/ionic-team/capacitor/issues/5606#issuecomment-1122461215

Upvotes: 5

Callan
Callan

Reputation: 1233

I think you are subscribing wrong. It should be 'ionBackButton'. Like it says in the documentation (here):

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

Upvotes: 0

Related Questions