ㅡㅡㅡㅡㅡㅡㅡㅡㅡ我解决了ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
// Marshmallow below
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (view.canGoBack()) {
view.loadUrl(url);
return true;
}
return false;
}
// Nougat above
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.isRedirect()){
view.loadUrl(url);
return true;
}
return false;
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
后退按钮不起作用, 它工作得很好,我没有更新代码。
如果我按下后退按钮,它应该在页面回家时关闭。 但是,当我按下后退按钮时,我的代码应用程序将被关闭。
我认为Back代码(goBack,canGoBack)无法正常工作。
错误代码
E/ViewSystem: ViewRootImpl #2 Surface is not valid.
E/File: fail readDirectory() errno=2
W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
enter code here
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
公共类MainActivity扩展了AppCompatActivity {
private WebView myWebView;
public static final String INTENT_PROTOCOL_START = "intent:";
public static final String INTENT_PROTOCOL_INTENT = "#Intent;";
public static final String INTENT_PROTOCOL_END = ";end;";
public static final String GOOGLE_PLAY_STORE_PREFIX = "market://details?id=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.goBack();
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://fpfriends.com");
}
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
// I tried below code too.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event); //
}
}
答案 0 :(得分:0)
尝试调用onBackPressed
@Override
public void onBackPressed() {
super.onBackPressed();
}
或像这样更改你的onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 1 :(得分:0)
你应该这样做,见下文
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if (myWebView.canGoBack())//test if your webview can go back more if not it will go to else
myWebView.goBack();
else
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}