1

I'm developing an hybrid app in Android. I want to show an alertDialog(instead of an alert/confirm JavaScript function) and when the user touch YES, return a true response to JavaScript, otherwise return false. Then if it's true I want to redirect to another URL.

Here's the 'binding JavaScript code to Android code':

public class WebAppInterface {
        Context mContext;
        boolean result;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public boolean showAlert(String title, String message) {
            new AlertDialog.Builder(mContext)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result = true;
                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result = false;
                        }
                    })
                    .show();
            return result;
        }
    }

And the JavaScript part:

if (Android.showAlert('Title example', 'Are you sure you wanna exit?') == true) {
    window.location = "http://newurl.com/example.html";
}

It actually works but not as it supposed to do. JavaScript receives the result when I open again the alertDialog and then redirect to the new URL, It needs to redirect at the moment the user touch the YES button.

I've done lot of research on this particular case and sadly found nothing.

Thank you.

2 Answers 2

1

Here is the Javascript Code. Add a function to navigate to a page. Like doNavigate:

<script type="text/javascript">
    function showAlert(toast) {
        navigate = Android.showAlert('Title example', 'Are you sure you wanna exit?');
    }

    function doNavigate(){
        window.location = "http://www.google.com"
    }
</script>

And here is the Android code for show alert. Call the javascript method from onClick positive button:

    @JavascriptInterface
            public boolean showAlert(String title, String message) {
                new AlertDialog.Builder(mContext)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                               ((Activity)mContext).runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    myWeb.loadUrl("javascript:doNavigate()");
                                }
                            });
                                result = true;
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result = false;
                            }
                        })
                        .show();
                return result;
            }

Keep a reference of the WebView in your WebAppInterface:

public static class WebAppInterface{

        Context mContext;
        boolean result;
        WebView myWeb;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c, WebView webView) {
            mContext = c;
            myWeb = webView;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Tested. Getting following error log: java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {41f8d5e0} called on Looper (JavaBridge, tid 1659) {41fe0a38}, FYI main Looper is Looper (main, tid 1) {41f8d5e0})
Please recheck the onClick method of AlertDialog. I changed that a while back to run code in UIThread. Tested in Galaxy S4 and simulator.
0

Probably when on click gets executed it doesnt immediately return the desired boolean value. I'm thinking the next time you open the dialog then the if statment gets executed.

Maybe you can try injecting the logic through the constructor using an interface or class. And when the positive button is clicked it will call a method of that interface setting window.location Hope it helps . Good luck

1 Comment

Thanks for the answer! I'm very used to PHP and lines of code getting executed one after each other and forgot that this isn't the case for Android. It returns 'result' before the user presses any button so the next time it retuns what the user pressed. I'm now trying to return the result using the isShowing method. Hopefully it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.