I'm trying to write code in java when the webview reaches a specific page for example index it should exit the app but I failed till now.
here is my code:-
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
String url = "file:///android_asset/www/index.html" ;
if (url.startsWith("file:///android_asset/www/index.html")) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
}
Update:- Problem has been solved
here is what I did:-
@Override
public void onBackPressed() {
String url = mWebView.getUrl();;
if (url.equals("file:///android_asset/www/index.html")) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
else {
mWebView.goBack();
}
}
hope it will help someone in future