java - fragment 内的 WebView 返回按钮

标签 java android

在 fragment 内,我使用WebView。当点击后退按钮

时,我只是查询 2 个选项

第一个 if; url.equals("www.google.com") [完美运行]

第二个 if; !url.equals("www.google.com") [不起作用]

我在 Debug模式下工作,但我的第二个 if 不起作用,即使 url 与“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);

最佳答案

在包含 fragment 的 Activity 中应用后退按钮

关于java - fragment 内的 WebView 返回按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490383/

相关文章:

java - 贾斯珀报告 : Some symbols from UTF-8 are lost after exporting to PDF

java - 如何使用 session ID 使 http session 无效

java - 在 Java 中使用 RxJava2 编写后续 API 请求

android - 如何在 Kotlin 中使用 MPAndroidChart 为条形图添加带有一些详细信息的标记?

java - android-bitmap 低质量问题

java - Java中的俄罗斯方 block 形状生成算法

c# - 如何在 Java、C# 和/或 C 中查找无线网络 (SSID) 的列表?

java - JDBC 查询抛出语法错误

java - 取消选中/取消选择 singleChoice ListView

android - 在 Android 上使用 Phonegap 通过蓝牙进行设备间通信?