0

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?

Sushant kunal
  • 341
  • 1
  • 2
  • 9

1 Answers1

2

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
}
Community
  • 1
  • 1
Sushant kunal
  • 341
  • 1
  • 2
  • 9
  • Indeed, please refer to the [doc](https://github.com/excilys/androidannotations/wiki/Injecting-Views). – WonderCsabo Jun 09 '15 at 15:37
  • @3mpty: no, injected views are not yet available in `@AfterInject` annotated methods, only in `@AfterViews` annotated methods. `@AfterInject` can be used for injected beans, prefs, rest, etc. – WonderCsabo Jun 16 '15 at 22:32
  • Yes, it was shorthand. Sorry about that. I mean that you can also init your code there, I didn't meant Views. Usually I'm using @AfterInject also when I need to init something that is not require Views or something that I need init before views. – 3mpty Jun 17 '15 at 05:50
  • Don't forget to select your answer as the correct answer. – ByteWelder Sep 07 '15 at 12:28