onBackPressed ไม่ได้ถูกเรียกเมื่อ PopupWindow แสดงอยู่

เฮ้พวก

ขณะนี้ฉันกำลังใช้ PopupWindow เพื่อแสดงเบราว์เซอร์ในแอป แต่พอกดปุ่มย้อนกลับกลับไม่ทำอะไรเลย ฉันใช้ PopupWindow ใน Fragment อื่น จากนั้นฉันใช้คำสั่งเพื่อตั้งค่า PopupWindow ใน FragmentActivity ของฉัน จากนั้นเมื่อฉันกดปุ่มย้อนกลับ ควรตรวจสอบว่า PopupWindow ถูกตั้งค่าไว้หรือไม่ และปิดทิ้งหรือไม่ อย่างไรก็ตาม มันไม่ได้ทำงานผ่าน onBackPressed ด้วยซ้ำ

PopupWindow ในส่วน:

--> เป็นที่ที่ฉันชี้ให้เห็นบรรทัดที่ทำให้แน่ใจว่า 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 ปิดก่อน ความช่วยเหลือใด ๆ ที่ชื่นชม


person Jordi Sipkens    schedule 07.01.2015    source แหล่งที่มา


คำตอบ (4)


ทำให้ PopupWindow ของคุณไม่สามารถโฟกัสได้:

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

ลบบรรทัดนี้ซึ่งซ้ำซ้อนด้วย:

popWindow.setFocusable(true);
person Simas    schedule 07.01.2015
comment
สุดยอด! คุณกอบกู้วันของฉัน - person Ramesh Akula; 18.09.2016
comment
ใช้งานไม่ได้บน Android 4.4.2 โดยมี new PopupWindows(Context); popupWindow.setFocusable(false); - person Jemshit Iskenderov; 23.02.2017
comment
ฉันต้องตั้งค่าพื้นหลังเป็นแบบไม่มีค่าว่าง อย่างน้อย popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); - person Jemshit Iskenderov; 23.02.2017
comment
แต่ตอนนี้แป้นพิมพ์จะไม่แสดงเมื่อคลิก EditText ): - person Stav Bodik; 22.05.2017

ฉันคิดว่าคุณควรกำหนดวิธีการคงที่ removePopupWindow(view v) ใน MainActivity และเรียกมันภายใน onBackPressed() เช่น MainActivity.removePopupWindow(popWindow); หวังว่ามันจะช่วยคุณได้

person SAM    schedule 07.01.2015
comment
onBackPressed ของฉันอยู่ใน MainActivity (FragmentActivity) แล้ว และไม่ได้ถูกเรียก นั่นเป็นปัญหาของฉันตอนนี้ เมื่อ PopupWindow ของฉันแสดงขึ้นมา มันจะไม่เรียก onBackPressed - person Jordi Sipkens; 07.01.2015
comment
ใส่ super.onBackPressed(); ตอนนี้ลอง - person SAM; 07.01.2015
comment
หากไม่ได้เรียก onBackPressed การเรียกเมธอด super ภายในนั้นจะไม่เปลี่ยนแปลงอะไรเลย - person Simas; 07.01.2015
comment
คุณเหนือฉันได้คำตอบที่ถูกต้องแล้ว! ขอบคุณนะ - person Jordi Sipkens; 07.01.2015

ตกลง คำถามนี้ถูกถามมานานแล้ว แต่ฉันคิดว่าฉันมีวิธีแก้ปัญหาที่ดีกว่า สิ่งที่ฉันทำคือเพิ่ม OnDismissListener ลงในป๊อปอัปของฉัน ใน Listener นี้ ฉันได้เพิ่มโค้ดที่ฉันต้องการดำเนินการเมื่อป๊อปอัปถูกปิด วิธีนี้ทำให้ Android ยังคงจัดการการเปิดและปิดป๊อปอัปได้ และฉันเพิ่งเพิ่มบรรทัดเดียวเพื่อให้มันใช้งานได้

นี่คือวิธีเพิ่ม:

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

คุณสามารถใช้สิ่งนี้

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();
      }
   }
});
person Sagittarius    schedule 05.02.2020