androidandroid-fragmentsandroid-popupwindow

onBackPressed not called when PopupWindow is showing


Hee guys,

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 in fragment:

--> 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();
        }
    });
}

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;
        }
    }
}

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.


Solution

  • Make your PopupWindow not focusable:

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

    Also remove this line which was redundant:

    popWindow.setFocusable(true);