0

I am having one activty, in which Webview is available. If I click on any URL in the webview, that url opens, but when I click the back button, it doesn't comes on the previous Webview page, but goes on the activity which is before that webview page.

Scenerio:- I am having a Xyz Activity, on some element of Xyz activity click it goes to a Webview activity which shows the loaded url on webview. on webview if I click on any url link, it opens, but when I click the back button, it goes back on the Xyz Activity and not on the Webview Activity. I want to come back on the Webview Activity and not on the Xyz Activity.

Can anyone Guide me for the same, as I am not able to find what should I use or implement for making it work as I need? Any help will be appreciated. Thanks in Advance...

EDit:-

The code is as follows:-

public class StoryDetail extends SherlockActivity
{
    WebView content;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.story_detail);

    BaseStory story = BaseStory.createFromCursor(c); // BaseStory is a class containing Database values...

    // Get the fields
    content = (WebView) findViewById(R.id.content);
    content.getSettings().setPluginState(PluginState.ON_DEMAND);
    content.getSettings().setJavaScriptEnabled(true);
    content.getSettings().setAllowFileAccess(true);
    content.setWebChromeClient(new WebChromeClient());
    content.setWebViewClient(new WebViewClient());

    String html = 
        "<html><head>" + 
            "</head>" +
        "<body>" +

            "<section id=\"content\">" +
                story.getContent() + 
            "</section>" +
        "</body></html>"; //story.getContent() contains title, image, and url link supporting HTML Format itself. 
    //On clicking url link the webview open that page in Webview.

    content.loadDataWithBaseURL("http://somelink/", html, "text/html", "UTF-8", "");

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(content.canGoBack()){
                content.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

}

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
  • possible duplicate of [Android WebView, how to code the back button?](http://stackoverflow.com/questions/6077141/android-webview-how-to-code-the-back-button) – Apoorv Jul 01 '14 at 06:28
  • @Apoorv Thanks for the link, it worked, but when it comes back on the Webview page, it doesn't shows any Html that was present in the Webview, any Idea how to include it again? – Pravinsingh Waghela Jul 01 '14 at 07:49

3 Answers3

5

Try this, it will serve your purpose

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

});

And for Android 2.2 and up do this:

@Override
public void onBackPressed()
{
    if(webView.canGoBack())
        webView.goBack();
    else
        super.onBackPressed();
}
Badrul
  • 1,582
  • 4
  • 16
  • 27
  • Well it works, but my WEbview Activity had a HTML tag in it, and when it comes back it doesn't shows anything in my Webview activity, a Blank screen is shown. What should I do to regain my HTML tag of my Webview activity? – Pravinsingh Waghela Jul 01 '14 at 08:05
1

Use this method.

As your target Webview declared:

WebView myWebView;

follow with below:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(myWebView.canGoBack()){
                myWebView.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

Update:

Take history by:

WebBackForwardList myWebBackForwardList = myWebView.copyBackForwardList();
String historyUrl = myWebBackForwardList.getItemAtIndex(myWebBackForwardList.getCurrentIndex()-1).getUrl();

and then on back button:

historyUrl.goBack();
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
0

The link is being opened into a new webview intent. That is why when you click the back button, the default event of back button which is "to go back one intent" is being called.

Overriding it like how Badrul does is the best way i beleive.

gorbos
  • 311
  • 1
  • 7