简体   繁体   English

防止返回按钮退出Cordova插件中的android应用

[英]Prevent the back button from quitting android app inside a Cordova plugin

I am creating a Cordova plugin based on the Android Camera2Video example: http://developer.android.com/samples/Camera2Video/index.html 我正在基于Android Camera2Video示例创建Cordova插件: http : //developer.android.com/samples/Camera2Video/index.html

My plugin code is working here: https://github.com/kmturley/cordova-plugin-media-custom 我的插件代码在这里工作: https : //github.com/kmturley/cordova-plugin-media-custom

When I press the Android back button within the WebView I can successfully control it's behaviour using the following JavaScript code: 当我按下WebView中的Android后退按钮时,我可以使用以下JavaScript代码成功控制其行为:

document.addEventListener('backbutton', function (e) {
    e.preventDefault();
    window.alert('back pressed');
}, false);

However when I press the same back button while the native plugin overlay is shown, the JavaScript is ignored and the app quits. 但是,当我在显示本机插件覆盖图时按同一后退按钮时,JavaScript将被忽略,并且应用程序退出。

How can I catch the back button event and use it to trigger my plugin hide() method? 如何捕获后退按钮事件并使用它触发我的插件hide()方法?

Here is a link to the Cordova code handling the JavaScript back button: https://github.com/apache/cordova-android/blob/4bf705a3d39b34400388265381a9975b246e3779/framework/src/org/apache/cordova/CoreAndroid.java#L231 https://github.com/apache/cordova-android/blob/4bf705a3d39b34400388265381a9975b246e3779/framework/src/org/apache/cordova/CordovaWebViewImpl.java#L549 以下是处理JavaScript后退按钮的Cordova代码的链接: https: //github.com/apache/cordova-android/blob/4bf705a3d39b34400388265381a9975b246e3779/framework/src/org/apache/cordova/CoreAndroid.java#L231 https:/ /github.com/apache/cordova-android/blob/4bf705a3d39b34400388265381a9975b246e3779/framework/src/org/apache/cordova/CordovaWebViewImpl.java#L549

In your activity, override the method onBackPressed() . 在您的活动中,重写onBackPressed()方法。 It is called when user taps on Back button. 当用户点击“后退”按钮时会调用它。

您可以重写onBackPressed,而是调用插件的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM