user3760741
user3760741

Reputation: 163

Unable to call a new Activity from Javascript

I trying to call a new Activity from my Webview. However, whenever i click on the specified <div> nothing happens. Please see my implementation below:

MainActivity.java

WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    myWebView = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(this, "Android");
    myWebView.loadUrl("http://.../hello.html");
}

@JavascriptInterface
public void newActivityCalled(){
        Intent intent = new Intent(this,NewAct.class);
        startActivity(intent);
}

hello.html

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript">

    $("#yoyo").click(function (){
        Android.newActivityCalled();
    });

</script>
</head>
<body>
<div id="yoyo"> New Activity Text </div>
</body>
</html>

Upvotes: 0

Views: 465

Answers (1)

user3760741
user3760741

Reputation: 163

For some reason jQuery onclick method does not work. However, if you switch over to pure javascript then the code works:

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript">
   function loadActivity(){
       Android.newActivityCalled();
  }
</script>
</head>
<body>
<div id="yoyo" onclick="loadActivity()"> New Activity Text </div>
</body>
</html>

Upvotes: 1

Related Questions