Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:44:34+00:00 2026-05-25T10:44:34+00:00

A button in my WebView is used to go back using the history.back() JavaScript

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:44:35+00:00Added an answer on May 25, 2026 at 10:44 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using onKeyDown for handling the back button but on pressing the back
I got my webview embedded but now I cannot make a back button for
I create a new Button object but did not specify the command option upon
I've tried putting Google's +1 button in WebView using the methods they describe. I've
i am using webview in activity and want to use option menu too. but
I'm trying to program a back button on a simple webView application, and I'm
I'm not highly familiar with javascript but I think this is the best way
i want to add a button to toolbar of webview without using IB ,so
I want to disable the left bar button when my webview is loading can
Printing button is not shown in the report toolbar when they are opened in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.