android webview memory leak 線索

http://stackoverflow.com/questions/3130654/memory-leak-in-webview



I conclude from above comments and further tests, that the problem is a bug in the SDK: when creating a WebView via XML layout, the activity is passed as the context for the WebView, not the application context. When finishing the activity, the WebView still keeps references to the activity, therefore the activity doesn't get removed from the memory. I filed a bug report for that , see the link in the comment above.

webView = new WebView(getApplicationContext());
share|edit
 
1  
ty for that getApplicationContext() actually worked on my memory leak when creating the WebView. But when i add the webview to another ViewGroup the memory Leak appears again. My wild guess is that adopts the parent's baseContext. Is there anyworkAround to that? I create the parent with getApplicationContext() too... so i guess i'm out of theories – weakwire Sep 20 '11 at 1:34
4  
Note that using the application context means that you won't be able to click on links in your webview, since doing so will result in a crash: "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" – emmby Jan 20 '12 at 23:47
 
@emmby Thanks for the hint, wasn't thinking of that, as I just needed the webview to display more complex html (beyond what you can do with a TextView), but no links in it. Therefore, I didn't come across the issue you mentioned, but thanks for pointing it out again. +1 – Mathias Lin Jun 18 '12 at 15:27
2  
Also, any time the webview attempts to create a dialog (e.g., to remember a password, etc), the webview will crash as it expects an activity context. – markshiz Aug 30 '12 at 18:13
1  
It crush app when webView want to display dialog for example asking "Do you want save passwaord" then it will crush :( – Gelldur Dec 30 '12 at 20:52

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章