WebViewClient 中的 Android 后退按钮不会返回到当前 Activity

发布于 2024-12-08 18:01:35 字数 1583 浏览 1 评论 0原文

我想让按下按钮显示网页,我使用 WebViewClient 和 WebChromClient 的标准方法,如下所示:

final Button revsButton = (Button) this.findViewById(R.id.buttonReviews);
revsButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        wb.setWebViewClient(new ShareWebViewClient());  
        wb.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Loading Reviews...");
                activity.setProgress(progress * 100); 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
        wb.getSettings().setJavaScriptEnabled(true);  
        wb.loadUrl(revString );  
        setContentView(wb);                         
    }
}); 


private class ShareWebViewClient extends WebViewClient {  
    public boolean shouldOverrideUrlLoading(WebView view, String url) {   
        view.loadUrl(url);   
        return true;  
    } 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
      }
} 

这一切都很好,问题是当我按下“后退”按钮时,它会完全退出我的 Activity 并返回到上一个 Activity页。我真正想要后退按钮做的是关闭 WebViewClient 并返回到带有按钮的显示。到目前为止,这就是我尝试做的事情,其中​​一半有效 - 它没有离开活动,但活动页面只是一个空白屏幕,而不是显示应该在那里的控件。

@Override
public void onBackPressed() {
    if (wb.getVisibility() == View.VISIBLE) {
        wb.setVisibility(View.INVISIBLE);
    } else {
        finish();
    }
}

I want to have a button press display a web page, I use the standard approach with a WebViewClient and WebChromClient like so:

final Button revsButton = (Button) this.findViewById(R.id.buttonReviews);
revsButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        wb.setWebViewClient(new ShareWebViewClient());  
        wb.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("Loading Reviews...");
                activity.setProgress(progress * 100); 
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
        wb.getSettings().setJavaScriptEnabled(true);  
        wb.loadUrl(revString );  
        setContentView(wb);                         
    }
}); 


private class ShareWebViewClient extends WebViewClient {  
    public boolean shouldOverrideUrlLoading(WebView view, String url) {   
        view.loadUrl(url);   
        return true;  
    } 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
          Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
      }
} 

This is all working great, the thing is when I press the Back button it exits my Activity entirely and returns to the previous page. What I really want the back button to do is to close the WebViewClient and return to the dispaly with the button on it. This is what I tried to do so far which half worked -- it didn't leave the activity, but the activity page was just a blank screen instead of showing the controls that should be on there.

@Override
public void onBackPressed() {
    if (wb.getVisibility() == View.VISIBLE) {
        wb.setVisibility(View.INVISIBLE);
    } else {
        finish();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-12-15 18:01:35

我想发生这种情况是因为您使用的是 WebViewClient 而不是使用标准启动浏览器:

Uri uri=Uri.parse(urlString);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);

在这种情况下,“后退”按钮将关闭浏览器活动,您将恢复主要活动。

在你的情况下,我会建议在 onBackPressed() - 恢复你的原始内容选择

setContentView(my_original_layout);

I suppose this happens because you're using WebViewClient rather than launching browser using standard:

Uri uri=Uri.parse(urlString);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);

In this case BACK button would close browser activity and you'll get back your primary activity.

In your case I would propose in onBackPressed() - restore your original content selecting

setContentView(my_original_layout);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文