ーーーーーーーーーーI solved itーーーーーーーーーーWebview back button(goback)does not work
// 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;
}
ㅡーーーーーーーーーーーーーーーーーーーーーーーーーー
The back button does not work suddenly、 It works very well、I have not any update the code。
If I pressed the back button、It should close when the page was home。 But My code application is closed wherever、when I press the back button。
I think Back code(goBack、canGoBack)does not work properly。
Error code
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;
public class MainActivity extends 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); //
}
}
Check this [link](http://www.androhub.com/android-advance -webview /)。 –