简体   繁体   中英

Returning to App from Webview on logout of web application

I have pasted a small snippet of my code that will allow the person to click on the webview and return back to my login app. The web application in webview should only do this if they click the logout inside the web application that runs in the webview . In other words it shouldn't return to the login app just because they clicked somewhere in the webview.

I have tried doing this using the onPageStarted and just can't get it to work. It ask for 3 parameters and Android Studio won't even compile it. It may be the way I'm trying to call it as I don't quite understand how to call that method, after reading the documentation.

Also, I tried to make a private class called myWebClient to call the onPageStarted but can't seem to call that one either correctly in my MainActivity .

Here's the working code that will take me back to my app on ANY click within the webview.

webView.setOnTouchListener(new View.OnTouchListener() {
        String checkurl;

        public boolean onTouch(View v, MotionEvent e) {

        Intent intent = new Intent(context, MainActivity.class); 
        startActivity(intent); 
        Toast.makeText(getApplicationContext(), checkurl, Toast.LENGTH_LONG).show();

        return false;
        }

        });

Here's what I tried which didn't work.

webView.setOnTouchListener(new View.OnTouchListener() {
            String checkurl;
            public boolean onTouch(View v, MotionEvent e) {
                public void onPageStarted( WebView view, String url, Bitmap favicon){
                    if (checkurl == "mylogoutURLgoes here") {

                        Intent intent = new Intent(context, MainActivity.class); 
                        startActivity(intent); 
                    }
                    super.onPageStarted(webView, url, favicon);
                }
                Toast.makeText(getApplicationContext(), checkurl, Toast.LENGTH_LONG).show();

            }

        });

Probably this is what you are looking for:

webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if ("myLogoutUrl".equals(url)){
            //do stuff
        }
        super.onPageStarted(view, url, favicon);
    }
});

Another solution will be to add Javascript interface to communicate with code.

This post should lead you in the right direction. Detect click on HTML button through javascript in Android WebView

You need to create buttons in Java and add javascript interface components in your web app to handle them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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