【问题标题】:onBackPressed not called when PopupWindow is showing显示 PopupWindow 时未调用 onBackPressed
【发布时间】:2015-01-07 11:19:21
【问题描述】:

大家好,

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

片段中的弹出窗口:

--> 是我指出确保 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 先关闭。任何帮助表示赞赏。

【问题讨论】:

    标签: android android-fragments android-popupwindow


    【解决方案1】:

    使您的 PopupWindow 不可聚焦:

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

    同时删除多余的这一行:

    popWindow.setFocusable(true);
    

    【讨论】:

    • 太棒了!你拯救我的一天
    • 不适用于 Android 4.4.2,new PopupWindows(Context); popupWindow.setFocusable(false);
    • 我必须将背景设置为非空可绘制对象,至少 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    • 但是现在点击 EditText 时键盘不会显示):
    【解决方案2】:

    我认为你应该在 MainActivity 中定义一个静态方法removePopupWindow(view v), 并在onBackPressed() 中调用它,例如MainActivity.removePopupWindow(popWindow); 希望对你有帮助。

    【讨论】:

    • 我的 onBackPressed 已经在 MainActivity (FragmentActivity) 中并且没有被调用。这就是我现在的问题,当我的 PopupWindow 显示时它不会调用 onBackPressed。
    • 把 super.onBackPressed();现在试试
    • 如果onBackPressed没有被调用,调用它内部的超级方法不会改变任何东西。
    • 我上面的先生得到了正确答案!还是谢谢。
    【解决方案3】:

    好的,这个问题很久以前就被问过了,但我想我有更好的解决方案。我所做的是在弹出窗口中添加OnDismissListener。在这个监听器中,我添加了我想在弹出窗口消失时执行的代码。这样,Android 仍然可以管理弹出窗口的打开和关闭,我只是添加了一行来使其工作。

    这是加一个的方法:

    yourAwesomePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // Your code on dismiss here
            }
    });
    

    【讨论】:

      【解决方案4】:
       popup.showAtLocation(popupView, Gravity.CENTER,0,0);
                      popupshowing=true;// define this as a global
      
      
      
       @Override
          public void onBackPressed() {
              if(popupshowing) {
                  popup.dismiss();
                 popupshowing=false;
      
      
      
              }
              else {
                  super.onBackPressed();
              }
      
          }
      

      【讨论】:

        【解决方案5】:

        你可以用这个。

        Dialog dialog = new Dialog(ActivityMain.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);   dialog.setContentView(R.layout.mydialog);
        dialog.setCancelable(true);
        
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
           @Override
           public void onDismiss(DialogInterface dialog) {
              try {
                 if (mScannerView!=null) {
                    mScannerView.stopCamera();
                 }
              } catch(Exception e){
                 e.printStackTrace();
              }
           }
        });
        

        【讨论】:

          猜你喜欢
          • 2019-02-18
          • 1970-01-01
          • 2015-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多