Android Annotations - setContentView called last

535 views Asked by At

I have a basic question. In the android annotations library, it creates activities with , which in turn extend the original activity. public final class HubActivity extends HubActivity implements HasViews, OnViewChangedListener {

private final OnViewChangedNotifier onViewChangedNotifier_ = new OnViewChangedNotifier();

@Override
public void onCreate(Bundle savedInstanceState) {
    OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
    init_(savedInstanceState);
    super.onCreate(savedInstanceState);
    OnViewChangedNotifier.replaceNotifier(previousNotifier);
    setContentView(layout.activity_hub);
}

if you see the last statement is the setContentView in onCreate method. Additionally, it calls the super.onCreate() method prior to that. Now, if I have written some code in the Activity onCreate method which is dependent on the view elements, it will not work, would it? How do we tackle this? Any design practices I am doing wrong here?

1

There are 1 answers

4
Sushant kunal On BEST ANSWER

Ok. I figured it out. Thanks to the question - AndroidAnnotations how to add init code after onCreate

The UI elements initialization and other wirings apart from the view binding should be done in the init method with an @AfterViews annotation

@AfterViews
protected void init() {
    // your custom code
}