1

I had to declare a static handler because of some Java nonsense with leaks.

static class ParsingCompleteHandler extends Handler {
    private final WeakReference<BackupActivity> mTargetActivity;
    ParsingCompleteHandler(BackupActivity targetActivity) {
        mTargetActivity = new WeakReference<BackupActivity>(targetActivity);
    }

    @Override
    public void handleMessage(Message msg) {
        BackupActivity targetActivity = mTargetActivity.get();
        targetActivity.updateDialog();
    }
};  

Elsewhere in the code (inside a runnable) I was trying to sendEmptyMessage() to this handler

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            lastBackupDataObject = getBackupDataObjectFromFile(file);   
            parsingCompleteHandler.sendEmptyMessage(0);
        }
    };
    Thread parsingThread = new Thread(runnable);
    parsingThread.start();

but since the sendEmptyMessage() method is not static (and the handler now is), obviously I can't do it. And I need to send a message to the handler because that's what it's there for. How do I do it?

TimSim
  • 3,936
  • 7
  • 46
  • 83
  • Although it is quite unusual to have what is basically a strategy class as a singleton, apparently that's what you're looking for here. Create a `public static final ParsingCompleteHandler INSTANCE = new ParsingCompleteHandler()` and refer to that. – William F. Jameson Aug 07 '14 at 12:54
  • 1
    Your code is also obviously broken: you can't expect to be able to dereference a `WeakReference` like that. You must guard for `null` and reinitialize. It is likely that what you refer to as "nonsense" is actually caused by some misunderstanding of the mechanisms you are using. – William F. Jameson Aug 07 '14 at 12:56
  • All I know is when the handler was not static, everything was working fine but Eclipse was warning me about leaks and said I had to make the handler static to avoid leaks. And the solution to that was dereferencing a WeakReference (whatever that means) and now my code is broken. But people who ask how to fix the leak with the non-static handler get told to do what I did, so I don't know what to do. – TimSim Aug 07 '14 at 12:59
  • What exactly does Eclipse warn you about? This seems to be an Android-specific warning. Can you refer me to a relevant SO question where the same subject is discussed? – William F. Jameson Aug 07 '14 at 13:03
  • BTW a WeakReference is not something you can use without at least an introductory level of understanding of the Garbage Collection mechanics in the JVM. – William F. Jameson Aug 07 '14 at 13:05
  • This is one of the questions that discusses this warning. http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler?rq=1 – TimSim Aug 07 '14 at 13:06
  • 1
    I see; you must not capture the enclosing instance, that is why the class must be static (or a top-level class). I still don't see why you would use a WeakReference, this is not a use case for it. – William F. Jameson Aug 07 '14 at 13:14
  • Yes, I just used the answer from other questions without understanding it. I guess I'll ask a new question about my original problem, and explain why the most suggested solution is not working for me, maybe someone will know. – TimSim Aug 07 '14 at 13:16
  • 1
    Basically, a WeakReference may be *cleared* by the Garbage Collector at any point, after which `get()` will return `null`. It is useful only for caches, where you can recreate the object. – William F. Jameson Aug 07 '14 at 13:17

1 Answers1

2

You can do this by making your handler object as class global object and than call this message this should work

eg.

public class SomeClass{

ParsingCompleteHandler  parsingCompleteHandler;


static class ParsingCompleteHandler extends Handler {
    private final WeakReference<BackupActivity> mTargetActivity;
    ParsingCompleteHandler(BackupActivity targetActivity) {
        mTargetActivity = new WeakReference<BackupActivity>(targetActivity);
    }

    @Override
    public void handleMessage(Message msg) {
        BackupActivity targetActivity = mTargetActivity.get();
        targetActivity.updateDialog();
    }
}; 

than do

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            lastBackupDataObject = getBackupDataObjectFromFile(file);   
            parsingCompleteHandler.sendEmptyMessage(0);
        }
    };
    Thread parsingThread = new Thread(runnable);
    parsingThread.start();