0

I want to implement a code in my WebView Fragment to go back to previous pages. I have found a solution here but I don't know where to add it in my WebView Fragment. Can someone help me with this? Should I add it in the MainActivity or in my WebView Fragment?

MyWebViewFragment.java:

package tkr.firstprojects.com.tkr;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebBackForwardList;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewFragment extends Fragment {

WebView webview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.web_fragment, container,
            false);

    Bundle bundle = getArguments();

    String url = bundle.getString("url");

    webview = (WebView) rootView.findViewById(R.id.webView1);

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setSupportZoom(true);


    webview.loadUrl(url);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }



    });

    return rootView;


 }
}

And here's the code that I want to add from the solution to go back to previous pages. Where should I add this?

webView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
    if(event.getAction() == KeyEvent.ACTION_DOWN)
    {
        WebView webView = (WebView) v;

        switch(keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                if(webView.canGoBack())
                {
                    webView.goBack();
                    return true;
                }
                break;
        }
    }

    return false;
 }
});
Community
  • 1
  • 1
  • 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:40

1 Answers1

1

In your Fragment Activity

@Override
public void onBackPressed() {
   //Get fragment and webview from the fragment
   webview.goBack();
}
aminner
  • 359
  • 3
  • 10
  • Hi thanks for your answer. I'm a newbie could you please tell me where should i add this to my code. Should i add it to the end or at the onCreate? – Ganesh Varma Jun 06 '16 at 19:03
  • The activity that adds the fragment is where that should go. If you're not dynamically adding the fragment, the override should be in the activity that draws the layout with the fragment in it. – aminner Jun 06 '16 at 19:13