0

http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/

And I want to make a back button function for go back in a website, how can I add it? Than it push the back button the application will exit. i want to back to previous page. thanks! sorry, im beginner and sry for english. please help.

BHH95
  • 1
  • 2
  • See here: http://stackoverflow.com/questions/6077141/android-webview-how-to-code-the-back-button/6077173#6077173 – FoamyGuy Aug 30 '13 at 22:51

2 Answers2

2

I am interpreting instead of exiting the application, you want to go back to the previous web page.

You can have something like this:

webView.setOnKeyListener(new View.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;
        }
    });

This listener would check if there's history of web page to go back to, otherwise it will exit.

mou
  • 323
  • 3
  • 10
  • thanks for the fast answer. which line should i put this snippet? :) – BHH95 Aug 30 '13 at 22:56
  • In the example http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/, you can put it in the onCreate method of UsingMyWebview before loading the url. – mou Aug 30 '13 at 22:58
  • Sorry, it dont know how i put this snippet to my code. can you write the whole code? – BHH95 Aug 31 '13 at 10:39
  • Why don't you paste your own code, and I will correct it if wrong :) – mou Sep 04 '13 at 17:51
0

Add "back" view on your layout and use webView.canGoBack() and webView.goBack()

getKonstantin
  • 1,220
  • 9
  • 14