The previous post is from here android studio webview get data and onfinish . I actually cannot get what I want from those answers. Maybe I should ask it more clearly. I actually used webview to load a content page. Then inside the content page, there are some links which are also local html created by myself. So the flow is like
android activity -> webview -> content page -> game page.
I actually want to retrieve a local variable marks
in the game page. So I created a exit
button, which is used to close the page and back to the android activity as well as carry the marks
back to the android activity.
So I searched some information myself, and found that window.close()
doesn't work. So I change the current URL to indicate that the exit button is clicked. And then extract the marks
from the URL and store it in the android activity. But it doesn't work with my codes below, I think it is probably because of the method is used, when I am in the content page. I hope someone could help to do this, as I really cannot find the problem. I have edited the codes as below:
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.loadUrl("file:///android_asset/index.html");
webview.setWebViewClient(new WebViewClient()
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains("?marks=")){
finish();} // close activity
else{
view.loadUrl(url);}
return true;
});
}
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() {
document.location.href= window.location.href +"?marks=" + marks;
}