android - 显示 PopupWindow 时未调用 onBackPressed

标签 android android-fragments android-popupwindow

嘿伙计们,

所以目前我正在使用 PopupWindow 来显示应用内浏览器。但是,当按下后退按钮时,它什么也不做。我在另一个 Fragment 中使用 PopupWindow,然后我使用一条语句在我的 FragmentActivity 中设置 PopupWindow,然后当我按下后退按钮时,它应该检查是否设置了 PopupWindow 并关闭它。然而,它甚至没有通过 onBackPressed 运行。

fragment 中的弹出窗口:

--> 是我指出确保 FragmentActivity 也获得 PopupWindow 的行的地方。

// Use webview for icons and website link.
public void inAppBrowser(String url){
    mCursor.moveToFirst();
    // Inflate View
    LayoutInflater layoutInflater = (LayoutInflater) ((MainActivity) MainActivity.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View inflatedView = layoutInflater.inflate(R.layout.browser_window, null, false);

    // Control View Childs.
    LinearLayout header = (LinearLayout) inflatedView.findViewById(R.id.filter_header);
    header.setBackgroundColor(Color.parseColor(color));
    Button cancel = (Button) inflatedView.findViewById(R.id.cancel);
    Button done = (Button) inflatedView.findViewById(R.id.done);

    // Set PopupWindow position.
    Display display = ((MainActivity) MainActivity.getContext()).getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    // Control PopupWindow.
    final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, true);
    popWindow.setAnimationStyle(android.R.anim.fade_in);
    popWindow.setFocusable(true);
    popWindow.setOutsideTouchable(true);

    popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 150);
    --> MainActivity.setPopupWindow(popWindow);

    // Control WebView
    WebView myWebView = (WebView) inflatedView.findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClientAdapter());
    myWebView.clearCache(true);
    myWebView.clearHistory();
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    MainActivity.setWebView(myWebView);
    if (url != null) {
        if (url.contains("http://") || url.contains("https://")) {

        } else {
            url = "http://" + url;
        }
        myWebView.loadUrl(url);
    } else {
        popWindow.dismiss();
        MainActivity.setPopupWindow(null);
        MainActivity.setWebView(null);
    }

    cancel.setVisibility(View.INVISIBLE);
    done.setText("Close");

    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popWindow.dismiss();
        }
    });
}

我的 onBackPressed 代码:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    //check if popupwindow is open
    Log.e(TAG, "Check if it runs through this section");
    if (popWindow != null) {
        if (myWebView != null && myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            popWindow.dismiss();
            popWindow = null;
            myWebView = null;
        }
    }
}

暂时忽略 WebView。这可能是将来的一个问题,但我希望 PopupWindow 先关闭。任何帮助表示赞赏。

最佳答案

使您的 PopupWindow 不可聚焦:

final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, false);

同时删除多余的这一行:

popWindow.setFocusable(true);

关于android - 显示 PopupWindow 时未调用 onBackPressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27818297/

相关文章:

android - 跳转到 Android Studio 调试器中的当前可执行行

android - gradle.properties vs buildscript ext 哪个更好

java - 从 Activity 获取 fragment 内的数据

android - PopupWindow 背景有时会变得透明和紫色

android - 如何在不实现 KeyboardView 表单的情况下自定义 keyPreview 和 popUpKeyboard 外观?

android - 样式化 Android PopupMenu 分隔线

java - 在 android 中重绘位图的更快方法?

android - 如何从 BroadCastReceiver 更新 Activity 的 UI

Android studio BUILD FAILED 在我的应用程序中显示错误 java.io.IOException : Could not delete path

java - 屏幕旋转后恢复 fragment 中微调器的位置