简体   繁体   中英

How to prevent GOBACK button for specific URL in Android Webview?

I have an web application that work on my Android WebView with out problem.

Here is my Android app launcher code:

public class MainActivity extends Activity {

WebView webView;

private String urlAddress = "http://www.YourDomainName.com/TheLoginPage";
private String loadingTxt = "Loading";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toast loadingMsg = Toast.makeText(this,
            loadingTxt, Toast.LENGTH_SHORT);
    loadingMsg.show();

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(urlAddress);
    webView.setWebViewClient(new myWebViewClient());
    webView.setInitialScale(1);
    webView.getSettings().setUseWideViewPort(true);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack()) {
                    webView.goBack();
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Every thing is working.

My Question: the default start page in the WebView is the web application login page. After a successful login process I want to prevent (build in) GO BACK button to back page to the default start page (login page) since I am successfully logged in?

Note: I will still use the GO BACK button to the rest of the web application or if I logout out again.

Here how I solved it, I have added if equals statement to compare the URL of the page that comes after the login process with the WebView URL, if they are identical then GO BACK is disallowed and a message "You are logged in", that way I prevent GO BACK. Here is the code and it works for me:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                String url = new String("http://www.YourDomainName.com/PageAfterSuccessfulLogin");
                String webUrl = new String(webView.getUrl());

                if (url.equals(webUrl)) {
                    Toast.makeText(this, "You are logged in",
                            Toast.LENGTH_SHORT).show();
                } else {
                    if (webView.canGoBack()) {
                        Toast.makeText(this, "Back",
                                Toast.LENGTH_SHORT).show();
                        webView.goBack();
                    }
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

Thanks maytham-maytham, it works charmly.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                String url = new String("https://www.domain name.com/index.php?logged=login%20successfully");
                String webUrl = new String(webView.getUrl());

                if (url.equals(webUrl)) {
                    Toast.makeText(this, "You are logged in",
                            Toast.LENGTH_SHORT).show();
                }
                else {


                    url = new String("https://www.domain name.com/main.php");
                    webUrl = new String(webView.getUrl());

                    if (url.equals(webUrl)) {
                        Toast.makeText(this, "Access Denied",
                                Toast.LENGTH_SHORT).show();
                    }


                 else
                    if (webView.canGoBack()) {
                        Toast.makeText(this, "Back",
                                Toast.LENGTH_SHORT);
                        webView.goBack();
                    }
                }
                return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

In which ever activity you want the back button to be disabled - DO THIS

@Override
public void onBackPressed(){
    return;
}

You have to add this as a global function.

As in your case you want your back button to work in some cases and not work in some-

You can do this inside the onBackPressed()

if(BackButton_Dont_Work_Condition)
  return

else 
  super.onBackPressed()

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