返回按钮关闭 WebView 应用程序

发布于 2024-11-18 17:11:54 字数 1086 浏览 2 评论 0原文

我有一个如下的应用程序,它作为 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);
}
}

“lastUrl”用于处理方向更改,并让用户在导航时位于同一页面上。

但我的问题是,如果用户点击某些链接然后点击返回按钮,应用程序就会关闭,而不是返回页面。我该如何处理?

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);
}
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2024-11-25 17:11:54

我通过处理返回并跟踪用户访问的 URL 解决了这个问题。

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文