Fork me on GitHub

java.lang.reflect.Method for Callback methods

Posted on 26 June 2014
Under Notes
Tagged
Java
java.lang.reflect.Method for Callback methods | Jay Rambhia’s Blog

I keep looking for ways to do things right. For calling some function of my activity, I’d pass activity to that class and call that particular method. I did not feel right doing this and I wanted to make my class more generic so I started looking for better approaches. I found this approach, where I can pass any function as reflect.Method and invoke it in that class. This seemed perfect, but as it turns out this also has some issues of its own.

Using getDeclaredMethod we can create Method instance. And pass it to the class for callback.

Activity class

import java.lang.reflect.Method;

public class MyActivity extends Activity {
    
    private SimpleButton button1;
    private SimpleButton button2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        button1 = new SimpleButton();
        try {
            Method method1 = null;
            method1 = MyActivity.class.getDeclaredMethod("onButton1Clicked",
                boolean.class);
            button1.setCallalbes(method1);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        button2 = new SimpleButton();
        try {
            Method method2 = null;
            method2 = MyActivity.class.getDeclaredMethod("onButton2Clicked", 
                boolean.class);
            button2.setCallalbes(method2);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void onButton1Clicked(boolean state) {
        // do something
    }

    public void onButton2Clicked(boolean state) {
        // do something
    }

}

SimpleButton class

Once we set the callback function using reflect.Method, we can call it with onTouchListener or any other functions.

public class SimpleButton {
    
    private Method callbackFunc;
    private boolean state;

    public SimpleButton() {

    }

    public void setCallables(Method cFunc) {
        callbackFunc = cFunc;
    }

    private void invokeCallbackFunctions(boolean state) {
        if (callbackFunc != null) {
            callbackFunc.invoke(state);
        }
    }
}

But the trouble doesn’t end here. I tested it and it was working fine. I uploaded it on PlayStore for Beta Testing. It just didn’t work. I downloaded the app from PlayStore, tried it and checked Log. It gave me methodNotFoundException. After a bit of googling, I found out this was due to proGuard. I didn’t add exceptions in its config. We had to add these methods in proguard-properties and after few tries it worked.

P.S. Building a real app is much complicated.

comments powered by Disqus


If you have some feedback or questions regarding this post, please add comments. I'd be happy to get some feedback.



If you liked this post, then you should follow Jay on twitter too. He would be excited to know that you like his work.