显示PopupWindow时不调用onBackPressed [英] onBackPressed not called when PopupWindow is showing
问题描述
伙计们,
因此,当前我正在使用PopupWindow来显示应用程序内浏览器.但是,当按下后退按钮时,它什么也没做.我在另一个Fragment中使用PopupWindow,然后使用一条语句在FragmentActivity中设置PopupWindow,然后当我按下后退按钮时,它应该检查PopupWindow是否设置并关闭.但是,它甚至没有通过onBackPressed运行.
So currently I'm using a PopupWindow to display an in app browser. However when pressing the back button it does nothing. I'm using the PopupWindow in another Fragment, then I use a statement to set the PopupWindow in my FragmentActivity and then when I press my back button it should check if the PopupWindow is set or not and dismiss it or not. However it doesn't even run through the onBackPressed.
片段中的PopupWindow:
PopupWindow in fragment:
->是我指出可确保FragmentActivity也获取PopupWindow的行的地方.
--> is where I point out the line which makes sure the FragmentActivity gets the PopupWindow as well.
// 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代码:
My onBackPressed code :
@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首先关闭.任何帮助表示赞赏.
Ignore the WebView for now. That might be a question in the future, but I want the PopupWindow to close first. Any help is appreciated.
推荐答案
使您的PopupWindow不能聚焦:
Make your PopupWindow not focusable:
final PopupWindow popWindow = new PopupWindow(inflatedView, size.x, size.y, false);
也请删除此多余的行:
popWindow.setFocusable(true);
这篇关于显示PopupWindow时不调用onBackPressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!