2

A button in my WebView is used to go back using the history.back() JavaScript call. I do not understand much of JavaScript, but after a bit of searching I found that we can use the addJavascriptInterface() method of a WebView.

Now, I intend to finish my Activity when the button is clicked. I landed up having something of this sort:

public class MyActivity extends Activity {
private static final String sTag = "MyActivity";

private WebView mWebContent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browser_page);


    mWebContent = (WebView) findViewById(R.id.webContent);

    mWebContent.getSettings().setJavaScriptEnabled(true);
    mWebContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebContent.getSettings().setBuiltInZoomControls(true);
    mWebContent.addJavascriptInterface(new JavaScriptInterface(), "history"); //history.back();

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        mWebContent.loadUrl(extras.getString("URL"));
    }
}

    public class JavaScriptInterface {
        JavaScriptInterface() {
        }

    public void back() {
        Log.v(sTag, "back pressed");
        MyActivity.this.finish();
    }
}
}

But unfortunately when the button is pressed the Activity doesn't finish, neither do I get anything in the Log.

Here is the html for the button :

<a href="javascript:void(0);">
   <img src="images/btn-go-back.png" onClick="history.back();" border="0" />
</a>

What am I doing wrong?

Thanks in advance.


Edit : Just wanted to clear the fact that changing the html is not an option for me. I have no power over the the html code :). I have to handle it in the application.


Edit: Tried changing history.back() to window.history.back() (locally), still no change.


Edit : I was experimenting by locally loading html conent and found out that if I change history.back() to say something like android_app.back() and register my JavaScriptInterface by the nameandroid_app it works fine. Why is that? Does that mean that we can't use history to register an interface? Does the developer docs mention this?

3 Answers 3

3

Read the Navigating web page history section of this article. It tells completely about the android webviews.It tells about how to make webviews handle history.

http://developer.android.com/guide/webapps/webview.html

Hope this helps.

3
  • Thanks for the link, Sreeram. But you see I have gone through the sites and have implemented exactly as written there. And Still my Activity wouldn't finish. Could you please go through my code, and see if there is something I'm missing? Aug 25, 2011 at 13:40
  • Hi Aki,Just tell me if you want to handle the page navigation in a webview or you want to do something from android side?
    – Sreeram
    Aug 29, 2011 at 9:19
  • @Aki I have read your edited message.Strange that you mentioned when you changed your javascriptinterface name from history to android_app.I have gone through the developer docs.there is nothing on this.
    – Sreeram
    Aug 29, 2011 at 9:39
1

Try <a href="#" onclick="window.history.back()"><img src="bla" /></a>

edit: just read you can't change the HTML code .. Don't see a solution then ..

7
  • I cannot change the html code, I'll have to do something from the application Aug 26, 2011 at 5:49
  • Just a very small comment, the API doc says: Do not use addJavascriptInterface() unless all of the HTML in this WebView was written by you: link. I understand that might not be an option ... Aug 26, 2011 at 6:01
  • Well it says that its dangerous as it can be used by someone to run malicious code in my app, but in my case that someone is a trusted guy, so it really doesn't matter. By the way, I tried changing it to window.history.back() (experimenting locally), and it still doesn't work. Aug 26, 2011 at 6:28
  • On the a href or on the img? Aug 26, 2011 at 6:44
  • changed the href to # and the img onClick to window.history.back() Aug 26, 2011 at 6:54
1

After a lot of searching and asking I have finally arrived at the conclusion that you cannot add a java script interface, using addJavascriptInterface (Object obj, String interfaceName), with the interface name as history (or perhaps any javascript keyword).

Maybe this is written somewhere in the developer docs or manual, but I couldn't find it though.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.