I've written a Utilloader class which provide a loader thread that load some data from net. in my activity I start a new loader thread. The thread then update the activity via handler.post().
class MyActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initUI();
UtilLoader.getInstance().load("url", new UtilLoad.OnLoadListener
{
public void onLoadSuccess(String response)
{
updateUI();
}
});
}
}
class UtilLoader
{
private Handler handler = new Handler();
private UtilLoader instance = new UtilLoader();
private ExecutorService executorService = Executors.newFixedThreadPool(3);
public interface OnLoadListener
{
public void onLoadSuccess(String response);
}
public UtilLoader getInstance()
{
return instance;
}
public load(String url, OnLoadListener onLoadListener)
{
executorService.submit(new Loader(url, onLoadListener));
}
public class Loader extends Runnable
{
private String url;
private OnLoadListener onLoadListener;
public Loader(String url, OnLoadListener onLoadListener)
{
this.url = url;
this.onLoadListener = onLoadListener;
}
public void run()
{
// LOAD DATA
handler.post(new Runnable() {
public void run() {
onLoadListener.onLoadSuccess(sb.toString());
}
});
}
}
}
Does this way of activity updating result in memory leaks by keeping a reference to the activity? If yes how must I change it, so there is no memory leaks?