Android WebView Optimization
WebView can help us to display html pages. However, improper use of webview may cause some problems. The following describes my optimization skills:
1. A webview activity can be used to create another process, which can be separated from the main process of our app. Even if webview has problems such as oom crash, it will not affect the main program, how to implement it is actually very simple, in androidmanifest. android: process = "packagename. the Code is as follows:
When running, you will find that there is another process, haha
2. webview creation is also skillful. It is best not to create webview in layout. using webview in xml, you can use the code of a viewgroup container to dynamically add view (webview) to the container, so that the webview can be destroyed in onDestory () and the memory can be cleared in time, in addition, you need to use applicationContext instead of activity context to create a webview. The activity object is no longer occupied when the webview is destroyed. You should know that webview and onDestory () must be destroyed when you exit () remove webview from viewgroup before calling webview. removeAllViews (); webview. destory (); the Code is as follows:
Create:
ll = new LinearLayout(getApplicationContext()); ll.setOrientation(LinearLayout.VERTICAL);wv = new WebView(getApplicationContext());
Destruction:
@Overrideprotected void onDestroy() {ll.removeAllViews();wv.stopLoading();wv.removeAllViews();wv.destroy();wv = null;ll = null;super.onDestroy();}
3. Further Optimization: after the activity is passively killed, it is best to save the webview status so that the user can see the previous status the next time he opens the activity. Well, that's the case, webview supports the saveState (bundle) and restoreState (bundle) methods, so it's easy. Haha, let's look at the Code:
Save status:
@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);wv.saveState(outState);Log.e(TAG, "save state...");}
Are you familiar with this? haha
Recovery status:
In the onCreate (bundle savedInstanceState) of the activity, you can use it as follows:
if(null!=savedInstanceState){wv.restoreState(savedInstanceState);Log.i(TAG, "restore state");}else{wv.loadUrl("http://3g.cn");}
Try it out. The results are satisfactory and the recovery is quite successful! 32 likes !!!
However, I still have a question: After opening the back page in the circle of friends, the back page can be restored to the original location. I don't know how they do it. If the back is normal, the onSaveInstanceState (bundle) will not be called, we can't save the status normally. If you want to know, please reply to me. Thank you.
Well, write so much first.
The complete code is provided below, hoping to help you.
Package com. example. test;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. util. Log;
Import android. webkit. WebSettings;
Import android. webkit. WebView;
Import android. webkit. WebViewClient;
Import android. widget. LinearLayout;
Public class WebViewActivityextends Activity {
Private staticfinal String TAG = WebViewActivity. class. getName ();
WebView wv;
LinearLayout ll;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Ll = new LinearLayout (getApplicationContext ());
Ll. setOrientation (LinearLayout. VERTICAL );
Wv = new WebView (getApplicationContext ());
WebSettings setting = wv. getSettings ();
Setting. setJavaScriptEnabled (true );
Setting. setBuiltInZoomControls (true );
Setting. setAppCacheEnabled (true );
Setting. setDisplayZoomControls (true );
Ll. addView (wv );
SetContentView (ll );
If (null! = SavedInstanceState ){
Wv. restoreState (savedInstanceState );
Log. I (TAG, "restore state ");
} Else {
Wv. loadUrl ("http://3g.cn ");
}
Wv. setWebViewClient (new WebViewClient (){
@ Override
Publicboolean shouldOverrideUrlLoading (WebView view, String url ){
Log. d (TAG, "jump to:" + url );
Wv. loadUrl (url );
Returntrue;
}
@ Override
Publicvoid onPageStarted (WebView view, String url, Bitmap favicon ){
Super. onPageStarted (view, url, favicon );
Log. d (TAG, "pageStarted:" + url );
}
@ Override
Publicvoid onPageFinished (WebView view, String url ){
// TODO Auto-generated method stub
Super. onPageFinished (view, url );
Log. d (TAG, "pageFinished:" + url );
}
@ Override
Publicvoid onReceivedError (WebView view, int errorCode,
String description, String failingUrl ){
// TODO Auto-generated method stub
Super. onReceivedError (view, errorCode, description, failingUrl );
Log. e (TAG, "onReceivedError code:" + errorCode + ", failingUrl:" + failingUrl );
}
});
}
@ Override
Protected void onSaveInstanceState (Bundle outState ){
Super. onSaveInstanceState (outState );
Wv. saveState (outState );
Log. e (TAG, "save state ...");
}
@ Override
Public void onBackPressed (){
If (wv. canGoBack ())
Wv. goBack ();
Else {
Super. onBackPressed ();
// Process. killProcess (Process. myPid ());
}
}
@ Override
Protected void onDestroy (){
Ll. removeAllViews ();
Wv. stopLoading ();
Wv. removeAllViews ();
Wv. destroy ();
Wv = null;
Ll = null;
Super. onDestroy ();
}
}