Be careful with code abstractions
Developers often use abstractions simply as a good programming practice, because
abstractions can improve code flexibility and maintenance. However, abstractions
come at a significant cost: generally they require a fair amount more code that
needs to be executed, requiring more time and more RAM for that code to be mapped
into memory. So if your abstractions aren't supplying a significant benefit, you
should avoid them.
For example, enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
public class MainActivity extends AppCompatActivity{
public static final int STATE_NONE = -1;
public static final int STATE_LOADING = 0;
public static final int STATE_SUCCESS = 1;
public static final int STATE_ERROR = 2;
public static final int STATE_EMPTY = 3;
private @State int state;
public void setState(@State int state){
this.state = state;
}
@State
public int getState() {
return this.state;
}
@IntDef({STATE_EMPTY, STATE_ERROR, STATE_LOADING, STATE_NONE, STATE_SUCCESS})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}