I am using my own html code to show a html page in WebView. I want to close the current activity on click of Cancel Button.
My Code is like:
public class WebHtml extends Activity
{
String historyUrl, baseUrl;
URL url = null;
WebView mWebView;
String html = "<html>" +
"<body>" +
"<form id='form1' name='form1' method='post' action='get.html'>" +
"User <input type='text' name='user'> <br />" +
"Password <input type='password' name='user'> <br />" +
"<input type='submit' name='Submit' value='Login' />" +
"<input type='button' value='Cancel' name='Cancel' onClick='javascript:window.close();'' />" +
"</form>" +
"</body>" +
"</html>";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.webview);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadDataWithBaseURL(baseUrl, html, "text/html", "utf-8", historyUrl);
mWebView.setWebViewClient(new HelloWebViewClient());
setContentView(mWebView);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class HelloWebViewClient extends WebViewClient
{
public void onPageStarted(WebView view, String url, Bitmap bitmap)
{
Log.i("ShowForm", "onPageStarted()" + System.currentTimeMillis());
historyUrl = url.toString();
}
public void onPageFinished(WebView view, String url)
{
Log.i("ShowForm", "onPageFinisheded()" + System.currentTimeMillis());
baseUrl = url.toString();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
}
The thing is I am not able to make it close the current form activity. I had used (window.close(), form.close(), javascript:window.close() on onClick but they showed no effect) & (I tried window.unload() which made exception) Moreover System.exit() shuts the app off at the time activity first starts.
Please suggest me what should I do.
Thanks,
Haps.
Can you change the onClick of that cancel button to a URL? Something like
onClick="document.location.href='http://exitActivity';".Then in your
shouldOverrideUrlLoadingmethod, you can intercept that URL and exit the app.