From c9f18cb0e381b9a8f9c5f94e7a5f8022b2bb617b Mon Sep 17 00:00:00 2001 From: "976623696@qq.com" Date: Tue, 29 Sep 2020 15:11:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=82=E6=95=B0=20canhardw?= =?UTF-8?q?areback=20=EF=BC=8C=E7=89=A9=E7=90=86=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=94=AE=E6=98=AF=E5=90=A6=E7=94=9F=E6=95=88=EF=BC=8C=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/InAppBrowser.java | 9 ++++++++- src/android/InAppBrowserDialog.java | 25 +++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index a74d372..4cabff0 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -125,6 +125,7 @@ public class InAppBrowser extends CordovaPlugin { private static final String COLOR = "color"; private static final String BACKGROUND = "background"; private static final String BACKBUTTON = "backbutton"; + private static final String CANHARDWAREBACK = "canhardwareback"; private static final List customizableOptions = Arrays.asList(CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR, FOOTER_COLOR,BACKGROUND,COLOR); @@ -160,6 +161,8 @@ public class InAppBrowser extends CordovaPlugin { private boolean statusbar = true; // 标题栏 private boolean titlebar = false; + // 物理返回键是否生效 + private boolean canhardwareback = true; private String color = ""; private String background = ""; private boolean backbutton = true; @@ -757,6 +760,10 @@ public class InAppBrowser extends CordovaPlugin { if (backbuttonSet != null) { backbutton = backbuttonSet.equals("yes"); } + String canhardwarebackSet = features.get(CANHARDWAREBACK); + if (canhardwarebackSet != null) { + canhardwareback = canhardwarebackSet.equals("yes"); + } if (features.get(COLOR) != null) { color = features.get(COLOR); } @@ -841,7 +848,7 @@ public class InAppBrowser extends CordovaPlugin { }; // Let's create the main dialog - dialog = new InAppBrowserDialog(cordova.getActivity(),fullscreen ? android.R.style.Theme_NoTitleBar : android.R.style.Theme_Black); + dialog = new InAppBrowserDialog(cordova.getActivity(),fullscreen ? android.R.style.Theme_NoTitleBar : android.R.style.Theme_Black, canhardwareback); dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog; dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); if (fullscreen) { diff --git a/src/android/InAppBrowserDialog.java b/src/android/InAppBrowserDialog.java index e7b212f..21c01c6 100644 --- a/src/android/InAppBrowserDialog.java +++ b/src/android/InAppBrowserDialog.java @@ -31,10 +31,12 @@ import org.json.JSONObject; public class InAppBrowserDialog extends Dialog { Context context; InAppBrowser inAppBrowser = null; + boolean canHardwareBack = true; - public InAppBrowserDialog(Context context, int theme) { + public InAppBrowserDialog(Context context, int theme, boolean canHardwareBack) { super(context, theme); this.context = context; + this.canHardwareBack = canHardwareBack; } public void setInAppBroswer(InAppBrowser browser) { @@ -42,16 +44,19 @@ public class InAppBrowserDialog extends Dialog { } public void onBackPressed () { - if (this.inAppBrowser == null) { - this.dismiss(); - } else { - // better to go through the in inAppBrowser - // because it does a clean up - if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) { - this.inAppBrowser.goBack(); - } else { - this.inAppBrowser.closeDialog(); + if (canHardwareBack){ + if (this.inAppBrowser == null) { + this.dismiss(); + } else { + // better to go through the in inAppBrowser + // because it does a clean up + if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) { + this.inAppBrowser.goBack(); + } else { + this.inAppBrowser.closeDialog(); + } } } + } }