片段内的WebView Go Back按钮

时间:2018-03-26 11:29:09

标签: java android

在片段中,我使用 WebView 。我只是在后退按钮单击

时查询2个选项

首先if; url.equals(" www.google.com") [完美无缺]

第二个if; !url.equals(" www.google.com") [没有工作]

我在调试模式下工作,但我的第二个如果没有工作,即使网址与" google.com"并关闭应用程序!

Fragment.Java

@Override
public void onResume() {
    super.onResume();

    if (getView() == null) {
        return;
    }

    getView().setFocusableInTouchMode(true);
    getView().requestFocus();

    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {

                if(url.equals("https://www.google.com")){

                    //normal geri

                    final AlertDialog.Builder builder_question1 = new AlertDialog.Builder(getContext());
                    builder_question1.setTitle("Are you sure to close the app?");
                    builder_question1.setCancelable(true);
                    //POSITIVE BUTTON
                    builder_question1.setPositiveButton("yes",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            System.exit(0);

                        }
                    });

                    //NEGATIVE BUTTON
                    builder_question1.setNegativeButton("no",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //do something...
                            //and dismiss
                            dialogInterface.dismiss();
                        }
                    });


                    builder_question1.create().show();



                }

                if(!url.equals("https://www.google.com")){
                   if(webView.canGoBack()){
                       webView.goBack();
                   }

                }
            }
            return true;
        }
    });



}

WebViewClient

webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Toast.makeText(getContext(), ""+url, Toast.LENGTH_SHORT).show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onLoadResource(WebView view, String url) {


            super.onLoadResource(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
           // Toast.makeText(getContext(), "page loaded", Toast.LENGTH_SHORT).show();
            super.onPageFinished(view, url);
        }

    });
    webView.loadUrl(url);

3 个答案:

答案 0 :(得分:0)

在包含片段

的活动中应用“返回”按钮

答案 1 :(得分:0)

这是一个问题,当您尝试显示对话框时,由于对话框构建器中的上下文类型错误,应用程序第二次崩溃。

使用getContext()

将AlertDialog上下文从getActivity();更改为System.exit(0);getActivity().finish();
 final AlertDialog.Builder builder_question1 = new AlertDialog.Builder(getActivity());
                        builder_question1.setTitle("Are you sure to close the app?");
                        builder_question1.setCancelable(true);
                        //POSITIVE BUTTON
                        builder_question1.setPositiveButton("yes",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                getActivity().finish();

                            }
                        });


if(!url.equals("https://www.google.com")){
                   if(webView.canGoBack()){
                       webView.goBack();
return false;
                   }

                }

答案 2 :(得分:-1)

也许这有助于你

if(url.equals("https://www.google.com")){
     //normal geri
} else {
    if(webView.canGoBack()){
       webView.goBack();
    }
}