Cordova 后退按钮在使用自己的插件后销毁 CordovaActivity

Posted

技术标签:

【中文标题】Cordova 后退按钮在使用自己的插件后销毁 CordovaActivity【英文标题】:Cordova backbutton destroy the CordovaActivity after use own plugin 【发布时间】:2018-01-02 21:16:03 【问题描述】:

我开发了一个cordova插件,可以为pdf创建一个文件容器。我的插件有enterFullscreen()exitFUllscreen() 的功能。如果我切换到全屏并想要返回默认文件容器,我想为此使用后退按钮,但如果我按下后退按钮,应用程序将关闭。在我的 logcat 中,我收到以下消息:

这是我的插件代码:java

/**
 * Enters the fullscreen mode of a Container.
 *
 * @param Container _container The Container which should enter the fullscreen mode.
 * @param boolean _enableFullscreen True for enabling fullscreen mode, false for disabling.
 */
private void setContainerFullscreen(final Container _container,final boolean _enableFullscreen)

    Log.d(LOGTAG,"FileViewer -> enterFullscreen of container with id: " + _container.id());

    this.cordova.getActivity().runOnUiThread(new Runnable()
    
        @Override
        public void run()
        
            _container.enableFullscreen(_enableFullscreen);

            FrameLayout containerLayout = _container.containerFrameLayout();

            /* first we must call removeView() then addView(), because the updateView() function don't work with fullscreen mode.
            //todo I must check this later.
            call a exception.*/
            mainLayout.removeView(containerLayout);
            mainLayout.addView(containerLayout,containerLayout.getLayoutParams());
        
    );

这里是容器类

/**
 * Enables fullscreen mode for this Container.
 *
 * @param boolean _enable True for enabling fullscreen mode, false for disabling.
 */
public void enableFullscreen(boolean _enable)

    this.isFullscreen = _enable;

    if (this.isFullscreen && this.isVisible)
       // Sets the size of the parentlayout to fullscreen.
        this.containerFrameLayout.setLayoutParams(mainLayout.getLayoutParams());
        this.enableToolbar(true);
    
    else if (!this.isFullscreen && this.isVisible)
       // Sets the size back to the values before it went into fullscreen mode.
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(this.containerRect.right-this.containerRect.left,this.containerRect.bottom-this.containerRect.top);
        layoutParams.leftMargin = this.containerRect.left;
        layoutParams.topMargin = this.containerRect.top;
        layoutParams.gravity = 0;

        this.containerFrameLayout.setLayoutParams(layoutParams);
        this.enableToolbar(false);
    

这里是javascript代码:

// backbutton support for android in fullscreen mode
document.addEventListener("backbutton", Ext.bind(function(_event)
    if (this.fullScreen)
    
        //console.log("We are in fullscreen!");
        this.fileViewerPlugin.exitFullscreen();
        this.fullScreen=false;
        _event.preventDefault();
    
    else
    
        navigator.app.exitApp();
    
,this), false);

【问题讨论】:

【参考方案1】:

您看到的是back-button 的原始行为。您应该使用 override 这种行为,例如使用 this guide。

【讨论】:

我在我的 javascript 中这样做

以上是关于Cordova 后退按钮在使用自己的插件后销毁 CordovaActivity的主要内容,如果未能解决你的问题,请参考以下文章

当后退按钮按下销毁应用程序时,Flutter Android Platform EventChannel 不会停止

Cordova android后退按钮确认消息

如何使用 Cordova 相机插件拍摄多张照片

AngularJS 中的移动后退按钮与 Cordova/PhoneGap 不起作用

Cordova 后退按钮覆盖默认行为

当资源不是本地的(使用 http)时,不会触发 Cordova android 4.x 后退按钮事件