在网页视图Android应用程序后退导航 [英] Back Navigation in a Webview Android App

查看:131
本文介绍了在网页视图Android应用程序后退导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作网页视图Android应用程序。
我无法在我的应用程序来支持导航解决一个问题。我用这code和尝试了所有修改此进行。

I am working on a webview android app. I am unable fix a issue in my app to back navigate. I am using this code and tried all modifications can be made to this.

 public class DeviceActivity extends Activity
 {
 private WebView web;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    web = (WebView) findViewById(R.id.webView);
    web.getSettings().setJavaScriptEnabled(true); 
    web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    web.getSettings().setPluginsEnabled(false);
    web.getSettings().setSupportMultipleWindows(false);
    web.getSettings().setSupportZoom(false);
    web.setVerticalScrollBarEnabled(false);
    web.setHorizontalScrollBarEnabled(false);

    //Our application's main page will be loaded
    web.loadUrl("file:///android_asset/fbp/index.html");

    web.setWebViewClient(new WebViewClient() {
            @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
            public void onBackPressed (){

    if (web.canGoBack()) {
            web.goBack();       
    }
    else {
        //My exit alert code goes here.    

    }
 }
        });

}

 }

这并不向后导航,而是退出应用程序。
任何帮助将是AP preciated。

This doesn't navigate back but instead it exits the app. Any help would be appreciated.

推荐答案

活动添加此 code(不是在的onCreate( )或嵌套其他地方)

Add this in your Activity code (not in onCreate() or nested anywhere else)

@Override
public void onBackPressed() {
    if (web.copyBackForwardList().getCurrentIndex() > 0) {
        web.goBack();
    }
    else {
        // Your exit alert code, or alternatively line below to finish
        super.onBackPressed(); // finishes activity
    }
}

这将导航回到通过的WebView 历史堆栈,直到它是空的,然后执行默认动作(在这种情况下完成的活动)。

This will navigate back through the WebView history stack until it is empty and then perform the default action (in this case it finishes the activity).

您或许应该还设置WebViewClient有 shouldOverrideUrlLoading 收益真正或你就不会加载任何链接。

You should probably also set your WebViewClient to have shouldOverrideUrlLoading return true or you won't load any links.

这篇关于在网页视图Android应用程序后退导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆