Java – WebView What event handles (watches) URL changes – Android



The app does a login to a web application using WebView. Once in the webview, the webview appears to handle everything for you as it should based on the user’s clicks. However I need to review on each event if the URL changes to a specific logout URL.

How can I return the user to the app itself when the user logs out on the web application within the webview? I do not want the webview to stay as the active view.

I have tried WebViewClient.shouldOverrideUrlLoading and View.OnTouchListsner.

The class I tried to implement public but it didn’t allow me.

class myWebClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub

                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);


            }
        }

Here’s the code before I want to call the method to check the URL;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText phone = (EditText) findViewById(R.id.phone1);
        final EditText login = (EditText) findViewById(R.id.uname);
        final EditText pass = (EditText) findViewById(R.id.password);
        phone.requestFocus();
        final Context context = this;
        submit  = (Button)findViewById(R.id.submit);
        sbutton = (Button)findViewById(R.id.loginCred);
        chcred = (CheckBox) findViewById(R.id.cBox);
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textstring(phone, login, pass);
                encPhone = URLEncoder.encode(Phone);
                encLogin = URLEncoder.encode(Login);
                encPass = URLEncoder.encode(Pass);

                if (chcred.isChecked()) {
                    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
                    prefwrite(editor);
                }
                pushurl();
                clear(phone, login, pass);
            }
        });

        WebViewClient() //Right here is where I want to call it

Answer

This can help you: https://developer.android.com/reference/android/webkit/WebViewClient.html#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)

The event does not notify you when the url changes, but does when the page starts to load, at that point you only need to verify the current url.



Source: stackoverflow