简体   繁体   English

当Webview应用到达特定页面时,如何用Java编写代码,应该使用android后退按钮退出该应用?

[英]How to write code in java when webview app reaches specific page it should exit the app with android back button?

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. 当Webview到达特定页面(例如索引,它应该退出应用程序)时,我试图用Java编写代码,但直到现在我还是失败了。

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 希望对以后的人有帮助

You have used finish. 您已经用完了。 finish() only finishes the current activity, and not all the activity. finish()仅完成当前活动,而不是全部活动。

So u either have to finish all the previous activities of the application, as when u finish from this activity it exits from application. 因此,您要么必须完成应用程序的所有先前活动,否则当您完成此活动时,它将退出应用程序。

Otherwise , u don't have a process to exit from the application. 否则,您没有退出应用程序的过程。 Remember, exit() doesn't work in android. 请记住,exit()在android中不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM