I am really confused by the android.os.Handler mechanism in Android. Why does the message handling use int to encode the type of message? After reading Effective Java, where I have learned to favor Enum over int for these kind of types, I feel very insecure how to proceed here.
I want to use the state pattern, defining different state classes which subclass from Handler in order to deal with different messages from the View.
public class ReadyState extends Handler {
@Override
public void handleMessage(Message msg, int what) {
// ...
}
}
Do I have to encode my states with int, would one use an Enum with fields:
enum Action {
START(0), STOP(1), BACKFLIP(2);
int code;
public Action(int code) { this.code = code }
}
Are there alternative approaches?
It takes an int as a parameter because the authors of the library can’t put your enum in there, they don’t even know what it’s called!