java.lang.reflect.Method for Callback methods
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
SimpleButton class
Once we set the callback function using reflect.Method
, we can call it with onTouchListener or any other functions.
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.
Playing around with Android UI
Articles focusing on Android UI - playing around with ViewPagers, CoordinatorLayout, meaningful motions and animations, implementing difficult customized views, etc.