Reputation: 974
I am trying to interact with a WebForms site via a WebView inside an Android application.
I need to include a javascript interface to be able to send messages to the webview. However whenever a page does a partial postback, the interface gets removed and I get the javascript error "MyWebHost is undefined" when trying to access any of the functionality
Web Client:
public class MyWebviewClient extends WebViewClient
{
private View BusySpinner;
public String BaseUrl;
public MyWebviewClient(View busySpinner, String baseUrl)
{
BusySpinner = busySpinner;
BaseUrl = baseUrl;
}
@Override
public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
super.onPageStarted(view, url, favicon);
BusySpinner.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(url==null) { return;}
if(url.contains("token_capture"))
{
view.loadUrl(BaseUrl);
return;
}
view.addJavascriptInterface(new WebAppInterface(view.getContext()), "MyWebHost");
BusySpinner.setVisibility(View.GONE);
}
public class WebAppInterface
{
public Context Context;
public WebAppInterface(Context context)
{
Context = context;
}
@JavascriptInterface
public void HandleButtonPress()
{
Toast.makeText(Context, "The user pressed the button in the web view", Toast.LENGTH_LONG);
}
}
}
sample interaction
<!-- works after page load, buy not after a partial postback -->
<div onclick="MyWebHost.HandleButtonPress();"> Click Me</div>
Upvotes: 0
Views: 22