开发者

Return button close the WebView app

开发者 https://www.devze.com 2023-03-18 05:25 出处:网络
I have a application as below that runs as FullScreen.NoTitleBar: public class BrowserActivity extends Activity {

I have a application as below that runs as FullScreen.NoTitleBar:

public class BrowserActivity extends Activity {
private String lastUrl = "http://www.google.com";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    WebView web = (WebView) findViewById(R.id.webview);

    WebSettings settings = web.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(false);
    settings.setSupportMultipleWindows(false);
    settings.setSupportZoom(false);
    settings.setPluginsEnabled(true);

    web.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            lastUrl = url;
            view.loadUrl(url);
            return true;
        }
    });
    web.setVerticalScrollBarEnabled(false);
    web.setHorizontalScrollBarEnabled(false);

    web.loadUrl(lastUrl);
}
开发者_JS百科}

The "lastUrl" is used for handle orientation changes and let the user on the same page that he was while navigating.

But my problem is, if the user follow some links then hit the return button, the application just closes instead of returning a page back. How can I handle it?


I solved it by handling returns and keeping track of the URLs visited by the user.

public class BrowserActivity extends Activity {
    private Stack<String> urls = new Stack<String>();;

    private String lastUrl = "http://www.google.com/";
    private WebView web;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        try {
        web = (WebView) findViewById(R.id.webview);

        WebSettings settings = web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(false);
        settings.setSupportMultipleWindows(false);
        settings.setSupportZoom(false);
        settings.setPluginsEnabled(true);

        web.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                urls.push(lastUrl);
                lastUrl = url;
                return false;
            }
        });
        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);

        web.loadUrl(lastUrl);
    }

    public boolean onKeyDown(int keyCode, KeyEvent evt) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (urls.size() > 0) {
                lastUrl = urls.pop();
                web.loadUrl(lastUrl);
            } else
                finish();
            return true;
        }

        return false;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消