0

I want to implement back button for my webview fragment, tried different answers on stackoverflow, couldnt get it to work with my code.

Below is my code.

public class LoginFragment extends Fragment {

    public LoginFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_login, container, false);

        WebView webView = (WebView) rootView.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://example.com/");

        return rootView;
    }
}

I even tried this, found it at developer.android.com

public boolean WebViewGoBack() {
    if(webView.canGoBack()){
       webView.goBack();
       return true;
    }
    return false; 
}

Thanks for any help.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Gururaju Hiddenx
  • 199
  • 1
  • 4
  • 19
  • Possible duplicate of [How to go back to previous page if back button is pressed in WebView?](http://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-back-button-is-pressed-in-webview) – FelixSFD Mar 03 '17 at 15:48

2 Answers2

0

Try this:

@Override
public void onBackPressed() { 
    if (webView.canGoBack()) { //Gets whether this WebView has a back history item.
        webView.goBack(); //this takes to the previous page
    } else { 
        super.onBackPressed();// it finishes the current activity
    }
}
Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Could you please [edit] in an explanation of why/how this code answers the question? Code-only answers are discouraged, because they are not as easy to learn from as code with an explanation. Without an explanation it takes considerably more time and effort to understand what was being done, or the changes made to the code. The explanation is important both for people attempting to learn from the answer and those evaluating the answer to see if it is valid, or worth up voting. – Makyen Feb 21 '15 at 03:18
0
public class LoginFragment extends Fragment {

public LoginFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_login, container, false);

    WebView webView = (WebView) rootView.findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("http://example.com/");

    webView.setOnKeyListener(new OnKeyListener(){

        public boolean onKey(View v, int keyCode, KeyEvent event) {
              if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }
                return false;
        }
    });
    return rootView; 
}       
}
}
Gururaju Hiddenx
  • 199
  • 1
  • 4
  • 19
Mounir Elfassi
  • 2,242
  • 3
  • 23
  • 38