Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "Go Back" function in WebView inside Fragment?

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?

like image 996
Markus Rubey Avatar asked May 17 '12 07:05

Markus Rubey


People also ask

How do I go back in WebView?

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.

How can we prevent back in fragment?

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. }


Video Answer


2 Answers

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();     } } 
like image 62
Roman Black Avatar answered Sep 28 '22 02:09

Roman Black


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;         }     }); 
like image 21
XZandr Avatar answered Sep 28 '22 03:09

XZandr