0

I created a WebView using Fragment; here is my code:

public class MyWebView extends Fragment {

    private WebView webView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle args = getArguments();
        String url = args.getString("url");
        webView = (WebView) getView().findViewById(R.id.webView);

        webView.setWebViewClient(new MyBrowser());

        WebSettings webSettings = webView.getSettings();
        webSettings.setSaveFormData(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);

        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(url);
    }

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

        View view = inflater.inflate(R.layout.web_view, container, false);

        view.setOnKeyListener(new OnKeyListener() {
              @Override
              public boolean onKey( View v, int keyCode, KeyEvent event ) {
                  if (keyCode == KeyEvent.KEYCODE_BACK) {
                      if (webView.canGoBack()) {
                          webView.goBack();
                          return true;
                      }
                  }
                  return false;
              }
         });

        return view;        
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
           return true;
        }
    }
}

I tried to override onBackPressed() but it seems not to work with Fragment. So I used setOnKeyListener() but it doesn’t work. Can you tell me why the back button closes the app instead of going back to the previous web page?

Demitri
  • 13,134
  • 4
  • 40
  • 41
smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • I think here you can found more information... http://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-back-button-is-pressed-in-webview – PaytoN Dec 01 '14 at 17:41
  • @PaytoN that url refer to use activity not fragment – WardaLyn Mar 11 '15 at 10:55
  • back button takes you out of the current screen you have opened, in case of web view, doesnt matter you are on which page in web view, for mobile its just one screen, (Fragment or activity) so while pressing back it clears the current screen and goes back, doesnt matter which webpage link you are in.. – Aalap Patel Jan 29 '18 at 21:35

1 Answers1

0

The Fragment doesn't receive a callback of the OnBackPressed event. You'll have to make your Activity state aware when your fragment is displayed and communicate back and forth between the activity and the fragment.

You could make your code you're calling in on key a public boolean function for instance and if the fragment is present you interact with it, you continue to return true or false in a similar way which allows you to know on the activity if you should continue with it naturally if there are no more pages to go back.

Bryan Dormaier
  • 800
  • 6
  • 10