Webview return the history

1

As I can do for when I press%% of% back, it does not send me in the initial activity of the app for example (If in my webview I had open link 1,2,3,4 .. for each time I pressed the back button should return 3.2,1 ..) but direct me in the initial activity.

I'm using webview in a fragment!

here is my code:

    WebView webView;

String urls="http://www.*********.com/";
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.informacion, container, false);

    webView = (WebView) view.findViewById(R.id.webView);
    webView.loadUrl(urls);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.setWebViewClient(new WebViewClient());

    return view;
    
asked by user62207 21.02.2018 в 19:48
source

1 answer

-1

The fact that you are in Fragment is irrelevant, what you should do is implement that when your device detects the "back" key, check in WebView if you have a page load history and can return , if you arrive at the last instance you can close your application by finish(); :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean response;
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        if(webView.canGoBack()){
            webView.goBack();
            response = true;
        }else{
            finish();
            response = super.onKeyDown(keyCode, event);
        }
    }else{
        response = super.onKeyDown(keyCode, event);
    }
    return response;
}
    
answered by 22.02.2018 в 15:59