Reputation: 1754
I want to design an app based on a web-service, also I want to open some background page according to web-service data via WebView
to be able to track that pages in GoogleAnalytics
.
To accomplish this ideas I wrote the following singleton and It's work as I expected but there is strange memory leak behavior in this code that I assume it caused form WebView
bug!
public class SiteLoader {
private static SiteLoader mSiteLoader;
private WebView mWebView;
private Context c;
public enum LoaderUri {
MUSIC_URI, VIDEO_URI, BOOK_URI
}
private SiteLoader(Context c) {
this.c = c.getApplicationContext();
}
public static SiteLoader getInstance(Context c) {
if (mSiteLoader == null)
mSiteLoader = new SiteLoader(c);
return mSiteLoader;
}
public void trackUri(LoaderUri uri, String extraData) {
// Load new progress
switch (uri) {
case MUSIC_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/music/?id=" + extraData);
break;
case VIDEO_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/video/?id=" + extraData);
break;
case BOOK_URI:
getWebView().loadUrl(Constants.HOST_PREFIX + "/book/?id=" + extraData);
break;
}
}
public void destroyTracker() {
if (mWebView != null) {
mWebView.destroy();
mWebView = null;
}
}
private WebView getWebView() {
if (mWebView == null) {
mWebView = new WebView(c);
mWebView.getSettings().setJavaScriptEnabled(true);
}
return mWebView;
}
}
Any ideas to how get rid from this situation or is there any other alternative way to tack the page directly in web-service
?
Test case activity:
public class TestActivity extends Activity {
@Override
public void onResume() {
super.onResume();
SiteLoader.getInstance(this).trackUri(LoaderUri.VIDEO_URI, "en");
}
@Override
public void onPause() {
super.onResume();
SiteLoader.getInstance(this).destroyTracker();
}
@Override
public void onCreate(Bundle mBundle) {
super.onCreate(mBundle);
Toast.makeText(this, "System is running", Toast.LENGTH_LONG).show();
}
}
Thanks in Advance...
Upvotes: 0
Views: 140
Reputation: 4412
After discussing with the OP, it appears that there is no memory leak per se in this case; rather that the WebView is a memory intensive component that consumes a relatively high amount of RAM when used.
Upvotes: 1