Reputation: 3090
I am using android annotations and have some code that I need to execute in the onResume() function in my activity.
Is it safe to just override the onResume function from the android annotation activity (ie with @EActivity)?
Upvotes: 2
Views: 2302
Reputation: 515
You can bind your custom class with lifecycle component of android. It holds life cycle information of android component so that your custom class observe lifecycle changes.
public class MyObserver implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void connectListener() {
...
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void disconnectListener() {
...
}
}
myLifecycleOwner.getLifecycle().addObserver(new MyObserver());
Upvotes: 0
Reputation: 887
Yeah. Just call super.onResume()
and then add your code.
I'd do it just like their on create example here: https://github.com/excilys/androidannotations/wiki/Enhance-activities
Upvotes: 2
Reputation: 12207
Yeah, you should use these lifecycle methods just like with plain Android activities. There is one thing though: injected View
s are not yet available in your onCreate
method, this is why @AfterViews
exist:
@EActivity(R.layout.views_injected)
public class ViewsInjectedActivity extends Activity {
@ViewById
Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// myButton is not yet available here
}
@AfterViews
void setupViews() {
// myButton is first available here
myButton.setText("Hello");
}
@Override
protected void onResume() {
super.onResume();
// just as usual
}
}
Upvotes: 5