Skip to content

Commit 33eaf21

Browse files
RickyNotaroclaude
andauthored
Fix Android back button closing app instead of navigating back in WebView (#52)
onBackPressed() is deprecated and no longer called on Android 13+ (targetSdkVersion 36). Replace with OnBackPressedCallback registered on OnBackPressedDispatcher so the WebView back navigation works with the modern back gesture system. https://claude.ai/code/session_01N8qksHxWt2qBSM6SYPUiss Co-authored-by: Claude <noreply@anthropic.com>
1 parent e43a315 commit 33eaf21

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

APK/android/app/src/main/java/com/communauto/tools/MainActivity.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import android.os.Bundle;
44
import android.webkit.WebView;
5+
import androidx.activity.OnBackPressedCallback;
56
import com.getcapacitor.BridgeActivity;
67

78
public class MainActivity extends BridgeActivity {
89
@Override
910
protected void onCreate(Bundle savedInstanceState) {
1011
registerPlugin(WebViewCookiePlugin.class);
1112
super.onCreate(savedInstanceState);
12-
}
1313

14-
@Override
15-
public void onBackPressed() {
16-
WebView webView = getBridge().getWebView();
17-
if (webView.canGoBack()) {
18-
webView.goBack();
19-
} else {
20-
super.onBackPressed();
21-
}
14+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
15+
@Override
16+
public void handleOnBackPressed() {
17+
WebView webView = getBridge().getWebView();
18+
if (webView.canGoBack()) {
19+
webView.goBack();
20+
} else {
21+
setEnabled(false);
22+
getOnBackPressedDispatcher().onBackPressed();
23+
}
24+
}
25+
});
2226
}
2327
}

0 commit comments

Comments
 (0)