user3578109 user3578109 - 5 days ago 5
Android Question

How to use go back in sites correct?

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!

Answer

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;
Comments