In my application I have a thread and a corresponding handler to update the UI. This thread will be started from my main activity, and the Thread takes several parameters, including a context. This context is used this way:
Context c;
MessageHandler handler;
public BluetoothCommunicator(Context c) {
this.c = c;
handler = new MessageHandler(c);
}
In my run() method I post some data to this handler, when a special event has occured. In my handleMessage() method I create some ProgressDialogs, in order to inform the user that a operation is ongoing.
@Override
public void handleMessage(Message m) {
String message = (String) m.obj;
//Getting files
if (message.equals("0")) {
folder.appendToLogFile(new Date().toString(), "Incoming File From: " + deviceName);
pd = new ProgressDialog(c);
pd.setTitle("Please Wait..");
pd.setMessage("Retrieving file from " + deviceName);
pd.setCancelable(false);
pd.show();
}
As I understand I cannot create a ProgressDialog with getApplicationContext(), but I need the Activity Context.
The way I coded this will cause a memory leak in my applicaion, according to MAT

The way I see this trace is that the c corresponds the the object c I initalize in my constructor. Is this right?
How can I go around this problem? No an AsyncTask is not relevant because the problem is in the Context.
When the Thread finishes (ie: the
run()method is complete) just set variablecto null. That should take care of your memory leak.