【问题标题】:Android Webview, back button does not work when a new link is opened through the webviewAndroid Webview,通过webview打开新链接时后退按钮不起作用
【发布时间】:2015-10-16 19:45:58
【问题描述】:

我有一个带有本地 html、css 和 javascript 文件的 android 应用程序。

其中一个页面包含图片列表,这些图片附有指向外部网站的链接。

每当我点击链接时,URL 都会在 web 视图中打开。但是当我点击后退按钮时,它并没有回到上一页。

它适用于除使用外部链接的页面之外的所有页面。

MainActivity.java 如下所示:

public class MainActivity extends Activity {

WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.loadUrl("file:///android_asset/www/index.html");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient());
    // Force links and redirects to open in the WebView instead of in a browser
}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

MyAppWebViewClient.java 看起来像这样:

public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("/menu")) {
        return true;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

}

【问题讨论】:

    标签: android webview android-webview back-button android-websettings


    【解决方案1】:

    看看我在我的应用中做了什么,你也可以使用它...

    @Override
        public void onBackPressed() {
    
            if (webView.canGoBack()) {
    
                webView.goBack();
    
            } else {
                if (backPressed) {
                    super.onBackPressed();
                    return;
                }
    
                this.backPressed = true;
                Toast.makeText(this, "Press Back One More Time To Exit", Toast.LENGTH_SHORT).show();
    
                new Handler().postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
                        backPressed = false;
                    }
                }, 2000);
    
            }
    
        }
    

    在此代码中,如果您的 webView 可以返回,它将返回,但如果不能返回,则会出现 Toast,要求用户再次按返回以退出,以防止意外返回......

    Handler.postDelayed() 用于设置 2 秒的时间限制....两秒后再次提示用户按回......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多