UPDATE: Solved! Problem was related to my Viewpager not WebView.
I'm trying to add a "Go Back" function to my WebView
which is inside a Fragment
. But I can't figure out how to:
public final class TestFragment extends Fragment { static WebView mWeb; private View mContentView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.webview, null); mWeb = (WebView)mContentView.findViewById(R.id.webview); WebSettings settings = mWeb.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWeb.getSettings().setBuiltInZoomControls(false); mWeb.loadUrl("myurl..."); mWeb.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) { mWeb.goBack(); return true; } return false; } }); } }
I also tried something like:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) { mWeb.goBack(); return true; } return super.onKeyDown(keyCode, event); }
Another solution but same problem:
@Override public void onBackPressed() { if(webView.canGoBack()) webView.goBack(); else super.onBackPressed(); }
Any ideas how to get this working?
Android App Development for Beginners This example demonstrate about How to enable back button in android webview. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Here is the new way you can manage your onBackPressed() in fragment with the new call back of activity: // Disable onBack click requireActivity(). onBackPressedDispatcher. addCallback(this) { // With blank your fragment BackPressed will be disabled. }
Perhaps its android restriction. Try to do this using handler.
public final class TestFragment extends Fragment { static WebView mWeb; private View mContentView; private Handler handler = new Handler(){ @Override public void handleMessage(Message message) { switch (message.what) { case 1:{ webViewGoBack(); }break; } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.webview, null); mWeb = (WebView)mContentView.findViewById(R.id.webview); WebSettings settings = mWeb.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(false); mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWeb.getSettings().setBuiltInZoomControls(false); mWeb.loadUrl("myurl..."); mWeb.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && mWeb.canGoBack()) { handler.sendEmptyMessage(1); return true; } return false; } }); } private void webViewGoBack(){ mWeb.goBack(); } }
You can check this code :
webView.canGoBack(); webView.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && webView.canGoBack()) { webView.goBack(); return true; } return false; } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With