เฮ้พวก
ขณะนี้ฉันกำลังใช้ 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 ปิดก่อน ความช่วยเหลือใด ๆ ที่ชื่นชม