0

I have an android activity that holds the webview and I have a page that contains a local variable marks. The local variable will be increased when user got the correct answer. In the webpage, there is a button called exit which is supposed to close the webpage and go back to the activity in android, and it should carry the local variable marks back to the activity too. I want to ask how the exit button can be done in the webpage to close the page and return local variable by using Javascript and how can the activity in android receive the local variable from the webpage.

My activity in android:

private WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setContentView(R.layout.activity_main);

    webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);

    try {
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("file:///android_asset/index.html");
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    setContentView(webview);
}

My exit button is a div:

<div class="exit" onclick="finish()">Exit</div>

I am going to use the finish() function to return the variable and close the webpage back to the activity in android.

function finish() {}
Alex
  • 3,689
  • 1
  • 21
  • 32
androidnewbie
  • 374
  • 1
  • 6
  • 23

3 Answers3

0

To notify the host application that a page has finished loading. Then Call onPageFinished()

webview.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
    }
});

SOURCE

sivaBE35
  • 1,876
  • 18
  • 23
  • But how the activity receives the local variable and how the Javascript can be done to return and call the onPageFinish() function in android activity? – androidnewbie Feb 22 '17 at 13:26
0

you can do one thing..On click on the exit call any url like http://../getmark?marks=2 and once url load in the webview finish/ exit from webview. In the activity

 webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // parse the url here to get marks
    }
});
bhaskar kurzekar
  • 238
  • 2
  • 14
0

Register a javascriptinterface to your webview in onCreate:

this.webview.addJavascriptInterface(new MyJSInterface(this), "Android"); 

Implement setCount method in your Activity:

public void setCount (int count) {
    //do what ever you want with count
}

Make a new JavascriptInterface-Class:

public class MyJSInterface {
    private YourActivity yourActivity = null;
    public MyJSInterface (YourActivity yourActivity) {
        this.yourActivity = yourActivity;
    }

    @JavascriptInterface
    public void invoke (int count)  {
          this.yourActivity.setCount(count);
    }
}

And in javascript:

function finish(marks) {
    if (Android !== undefined) {
        if (Android.invoke !== undefined)  {
            Android.invoke(marks);
        }
    }
}
A.D.
  • 1,412
  • 2
  • 19
  • 37