当我点击后退按钮时,网页没有显示最后的网页

时间:2015-07-21 07:38:59

标签: android

我想知道当用户在他/她的Android应用程序中使用Web视图时按下后退按钮时的后退操作。这是我用于活动的代码。现在请建议我在代码中应该更改什么,或者我应该在我的活动中添加不同的代码片段。

 public class Webview_activity extends Activity
    {




        ProgressDialog pbhg;
        WebView web;
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview11);

            getActionBar().setTitle("Notifications");
            getActionBar().setIcon(R.drawable.phnxlogg);




            web=(WebView)findViewById(R.id.webView1);
            web.getSettings().setJavaScriptEnabled(true);
            web.setLongClickable(false);
            web.setWebViewClient(new WebViewClient());

            TelephonyManager tmw = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String imeie = tmw.getDeviceId();


            //String value=getIntent().getExtras().getString("orderid");
            //Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG).show();

                SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
                String username = pref.getString(PREF_USERNAME, "null");
                String password = pref.getString(PREF_PASSWORD, "null");



                web.loadUrl("url");




    /*******BACK KEY OPERATION*********/

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && this.web.canGoBack()) {


                // web.loadUrl("http://www.mobitalktime.com/offers.php?id="+vh1+"&salt="+vh2+"&deviceId="+vh3);
                 this.web.goBack();
                return true;
            }

            return super.onKeyDown(keyCode, event);
        }

        /*******BACK KEY OPERATION*********/


    }

当我尝试按回按钮时,显示主页面,但实际上我想让用户显示他/她在点击回来时所经历的整个网页。

1 个答案:

答案 0 :(得分:0)

检查一下:

 @Override

        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // Check if the key event was the Back button and if there's history
            if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
                myWebView.goBack();
                return true;
            }
            // If it wasn't the Back key or there's no web page history, bubble up to the default
            // system behavior (probably exit the activity)
            return super.onKeyDown(keyCode, event);
        }