每次活动聚焦时,Webview都会加载相同的URL

时间:2016-02-05 09:09:50

标签: java android android-studio

我的应用中有一个Webview。当我打开我的应用程序并切换到另一个应用程序或只是转到主屏幕,然后重新打开我的应用程序。 Webview加载我为其设置的网址,以加载onCreate()方法,而不是我在关闭应用之前访问的网址。

我使用此方法在sharedprefrence的帮助下保存url: http://developer.android.com/training/basics/data-storage/shared-preferences.html

标准的savedUrl是它在第一个onCreate()方法中加载的标准Url,但它是否会在应用暂停时重置我写入的值?

一些日志:

02-05 09:46:57.443 24132-24132/com.example.okke.testwebapp02 D/SAVING_URL: http://www.remotepressure.com/
02-05 09:46:57.453 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-05 09:46:57.453 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-05 09:46:57.453 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'click' of null", source:  (1)
02-05 09:47:00.201 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [ImeAdapter.java:571] focusedNodeChanged
02-05 09:47:00.214 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [ImeAdapter.java:213] updateKeyboardVisibility: type [0], flags [0], show [true]
02-05 09:47:00.214 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [AdapterInputConnection.java:178] updateState [] [0 0] [-1 -1] [true]
02-05 09:47:00.465 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [ImeAdapter.java:571] focusedNodeChanged
02-05 09:47:00.489 24132-24132/com.example.okke.testwebapp02 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 24132
02-05 09:47:00.878 24132-24132/com.example.okke.testwebapp02 D/SAVING_URL: http://www.remotepressure.com/welcome-page
02-05 09:47:00.888 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-05 09:47:00.888 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-05 09:47:00.888 24132-24132/com.example.okke.testwebapp02 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'click' of null", source:  (1)
02-05 09:47:06.488 24132-24132/com.example.okke.testwebapp02 D/APP: PAUSE
02-05 09:47:06.638 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
02-05 09:47:06.735 24132-24138/com.example.okke.testwebapp02 W/art: Suspending all threads took: 19.298ms
02-05 09:47:06.735 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
02-05 09:47:09.386 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [InputMethodManagerWrapper.java:27] Constructor
02-05 09:47:09.387 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [ImeAdapter.java:241] attach
02-05 09:47:09.387 24132-24132/com.example.okke.testwebapp02 W/art: Attempt to remove non-JNI local reference, dumping thread
02-05 09:47:09.388 24132-24132/com.example.okke.testwebapp02 W/AwContents: onDetachedFromWindow called when already detached. Ignoring
02-05 09:47:09.389 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [InputMethodManagerWrapper.java:56] isActive: false
02-05 09:47:09.389 24132-24132/com.example.okke.testwebapp02 D/APP: CREATE
02-05 09:47:09.392 24132-24132/com.example.okke.testwebapp02 D/LOADING_URL: http://www.remotepressure.com
02-05 09:47:09.400 24132-24132/com.example.okke.testwebapp02 D/cr_Ime: [ImeAdapter.java:241] attach
02-05 09:47:09.404 24132-24132/com.example.okke.testwebapp02 D/APP: START
02-05 09:47:09.405 24132-24132/com.example.okke.testwebapp02 D/APP: RESUME`

保存网址:

public void onPageFinished(WebView view, String url) {
            if (fromLogin) {
                //String javascript = "javascript:(function(){ document.getElementById('mib').value = 'aaa'; })()";
                //String javascript = "javascript:document.forms[0].username.value = '"+test01+"';";

                mWebView.loadUrl("javascript:var x = document.getElementById('modlgn-username').value = '" + userName + "';");
                mWebView.loadUrl("javascript:var z = document.getElementById('modlgn-passwd').value = '" + passWord + "';");
            /*mWebView.loadUrl("javascript:(function(){"+
                    "l=document.getElementById('form-login-submit');"+
                    "e=document.createEvent('HTMLEvents');"+
                    "e.initEvent('click',true,true);"+
                    "l.dispatchEvent(e);"+
                    "})()");*/
                //var y = $document.getElementById('form-login-submit').click();");
                //mWebView.loadUrl("http://www.google.com/");
                mWebView.loadUrl("javascript: var y = document.getElementById('form-login-submit'); y.click();");
            }
            if (mWebView.getUrl() != null) {
                SharedPreferences sharedpref = Web.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpref.edit();
                String temp = mWebView.getUrl();
                Log.d("SAVING_URL", temp);
                editor.putString(getString(R.string.loaded_url), mWebView.getUrl());
                editor.commit();
            }

onCreate()

中加载网址
Log.d("LOADING_URL", getResources().getString(R.string.loaded_url));
mWebView.loadUrl(getResources().getString((R.string.loaded_url)));

1 个答案:

答案 0 :(得分:0)

如果您使用的是共享偏好而不仅仅是def validation_preprocessor(data, *args, **kwargs): # validate data by any of your cool-validation-frameworks if errors: raise ProcessingException(description='Something went wrong', code=400) manager.create_api( Model, methods=['POST'], preprocessors=dict( POST=[validation_preprocessor] ) ) ,那么

不应该使用editor.getString(R.string.loaded_url, "")吗?

修改
不是getResources().getString((R.string.loaded_url))
editor.getString(R.string.loaded_url, "")
我的不好