0

After a whole night spent in test (without any luck) I need some support with my interface. I'm working directly on the Android frameworks and I created a class that works as a Binder with a WeakHashMap to control the callbacks.

Here is the code:

MyCallback:

public interface MyCallback {

    public void fire();
} 

MyBinder:

public static WeakHashMap<String, MyCallback> mCallbacks =
    new WeakHashMap<String, MyCallback>();

public static void setup(MyCallback callback) {
    if(mCallbacks.get(callback.getClass().getName()) == null) {
        mCallbacks.put(callback.getClass().getName(), callback);
    }
}

public static void letsgo() {
    Log.d("size", " " + mCallbacks.size()); // IMPORTANT
    for (MyCallback cb : mCallbacks.values()) {
        cb.fire();
    }
}

These 2 classes are written into frameworks so I created 2 test applications with a simple class that implements my interface:

public class FirstApp implements MyCallback {
    public FirstApp() {
        MyBinder.setup(this);
    }

    @Override
    public void fire() {
        Log.d("app1", "fired");
    }
}

public class SecondApp implements MyCallback {
    public SecondApp() {
        MyBinder.setup(this);
    }

    @Override
    public void fire() {
        Log.d("app2", "fired");
    }
}

Ok at this point I made another class (all these 3 classes, so the 2 that implements the interface and the following one are written into different packages)

In this third class i just call: MyBinder.letsgo();

The issue I'm facing, and that I'm trying to solve since... 8/9 hours is that: If i run letsgo() on the third pack the logs shown 0 for the callbacks WeakHashMap size. if i run letsgo() on the second package it works but it only fires the callback in the same package. the same if i run it on the first package.

I tried also with HashMap instead of WeakHashMap since i red that objects must be referenced but without any luck. I wonder if someone can help me and let me go sleep :D Thanks!

iGio90
  • 3,251
  • 7
  • 30
  • 43
  • You did not provide all code.. We can only see the working snippets.. And I believe this problem doesn't have anything to do with packages.. – Tobrun Jun 20 '14 at 11:19
  • yes, this is all the relevant code! i figured out that every time i run setup a new istance of MyBinder is created. So it's just like there is 1 binder for each package instead of 1 for all! – iGio90 Jun 20 '14 at 11:22
  • I do not think you can use an interface this way. What is the ultimate goal you are aiming at. – Parth Kapoor Jun 20 '14 at 11:39

0 Answers0