0

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

Shihab
  • 29
  • 6
  • what is happening in your case? – walkmn Nov 17 '15 at 09:38
  • http://stackoverflow.com/questions/3226495/android-exit-application-code – Michael Jaison Nov 17 '15 at 09:56
  • 1
    @walkmn When my webview app is on homepage which is index.html and I press back button to exit the app nothing is happening – Shihab Nov 17 '15 at 10:30
  • @walkmn actually it goes to the visited pages until it reaches the beginning then I can exit the app. What I need is when it the user press back button on the index page it should exit the app – Shihab Nov 17 '15 at 10:41
  • check a variable of this `mWebView.canGoBack()`, when it is false – walkmn Nov 17 '15 at 10:43
  • If you want to help other readers, the best thing to do it post your solution as an answer to the question and then accept it, instead of putting the answer inside the question. That way, other visitors will be able to see that you have solved your problem. – Dan Hulme Nov 19 '15 at 12:59

1 Answers1

0

You have used finish. finish() only finishes the current activity, and not all the activity.

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.

Ashish Ani
  • 324
  • 1
  • 7
  • 1
    I thought that in case I'm building the app without using webview since webview app needs only one activity to run it – Shihab Nov 17 '15 at 10:27