How to close webpage when using Webview?

时间:2018-03-25 20:48:49

标签: android android-webview

I have an .apk, which opens a webpage using WebView in Android Studio, and there is only a game on the website, that loads automatically and uses JavaScript. It works fine, but my only problem is, when I tap the back button on the phone, the app shows a dialog if the user really want to exit. When I tap on the yes button, the app closes, but I can hear that the game is running in the background. Not just the music, the whole game.

When I open again the app, the previous game section is still running, but It loads the game again, like if I have opened it in a new window, and then there is two instances running at the same time, but the first one is not visible.

The only method which can fix it is to kill the app from the memory. Does anyone have an idea, what to do with this problem? I used finish(); to exit the app, this way:

@Override
public void onBackPressed(){
    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Are you sure you want to quit?");
    builder.setCancelable(true);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

0 个答案:

没有答案
相关问题