-1

I have my WebViewFragment and of course i want to go back in the site im browsing but i have the error:

The method onBackPressed() is undefined for the type Fragment

Why? I don´t understand that? What am i doing wrong? Here´s my full webview code i´m using.

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewFragment extends Fragment {
    WebView webView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Retrieving the currently selected item number
        int position = getArguments().getInt("position");

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

        // List of rivers
        String[] menus = getResources().getStringArray(R.array.menus);

        // Creating view corresponding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Updating the action bar title
        getActivity().getActionBar().setTitle(menus[position]);

        //Initializing and loading url in webview
        webView = (WebView)v.findViewById(R.id.webView); 
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(url);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient(){


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
              view.loadUrl(url);
              return true;
            }


        });
        webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }
        });

        return v;
    }
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }}

If i´m using:

public void onBackPressed() {
       Fragment webview = getFragmentManager().findFragmentByTag("webview");
       if (webview instanceof WebViewFragment) {
              boolean goback = ((WebViewFragment)webview).canGoBack();
              if (!goback)
                super.onBackPressed();
       }
 }}

I have the errors:

The method canGoBack() is undefined for the type WebViewFragment

The method onBackPressed() is undefined for the type Fragment

Thank you dudes for your help!

  • from webview do you want to switch to another screen..rght? – Born To Win May 06 '14 at 08:22
  • 2
    onBackPressed is a method of activity not of fragment. Take a look here http://stackoverflow.com/questions/10631425/android-webview-inside-fragment-how-to-add-goback – Blackbelt May 06 '14 at 08:23
  • Let´s say i have google.com i´m searching something and on the screen i have the search result but then i want to go back to google.com. I want to go back 1 step in the site. I´m new to android so maybe i´m using something wrong? If i´m using the onKeyDown code i have the same problem in line: `return super.onKeyDown(keyCode, event);` thank you – user3578109 May 06 '14 at 08:25
  • I tried that all i have always the problem with `The method ...() is undefined for the type Fragment` – user3578109 May 06 '14 at 08:27
  • Actually you can not do directly inside the fragment. The onBackPressed can be overridden in the FragmentActivity. – Born To Win May 06 '14 at 08:27
  • and i always have to remove the @Override function? – user3578109 May 06 '14 at 08:27
  • If i´m using `public void onBackPressed() { Fragment webview = getFragmentManager().findFragmentByTag("webview"); if (webview instanceof WebViewFragment) { boolean goback = ((WebViewFragment)webview).canGoBack(); if (!goback) super.onBackPressed();` then i have the following errors: `The method canGoBack() is undefined for the type WebViewFragment` and `The method onBackPressed() is undefined for the type Fragment` – user3578109 May 06 '14 at 08:30
  • added more infos @BornToWin – user3578109 May 06 '14 at 08:33
  • added more infos @blackbelt – user3578109 May 06 '14 at 08:34

1 Answers1

2

Found the solution what is working for me in my fragment.

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;
    }
});
return v;
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41