-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance Fragments
Since AndroidAnnotations 2.1
Prior to AndroidAnnotations 2.6, there was no support for fragment injection. However, we made sure that at least extending FragmentActivity instead of Activity didn't break AndroidAnnotations:
@EActivity(R.id.main)
public class DetailsActivity extends FragmentActivity {
}Since AndroidAnnotations 2.6
AndroidAnnotations supports both android.app.Fragment and android.support.v4.app.Fragment, and automatically uses the right APIs based on the fragment types.
To start using AndroidAnnotations features in a fragment, annotate it with @EFragment:
@EFragment
public class MyFragment extends Fragment {
}AndroidAnnotations will generate a fragment subclass with a trailing underscore, e.g. MyFragment_. You should use the generated subclass in your xml layouts and when creating new instance fragments:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/myFragment"
android:name="com.company.MyFragment_"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>Programmatically:
MyFragment fragment = new MyFragment_();You can now use all kind of annotations in your fragment:
@EFragment
public class MyFragment extends Fragment {
@Bean
SomeBean someBean;
@ViewById
TextView myTextView;
@App
MyApplication customApplication;
@SystemService
ActivityManager activityManager;
@Click
void myButton() {
}
@UiThread
void uiThread() {
}
@AfterInject
void calledAfterInjection() {
}
@AfterViews
void calledAfterViewInjection() {
}
}View injection and event listener binding will only be based on views contained inside the fragment. Note, however, that it's isn't currently the case for
@EBeaninjected inside fragments: they have access to the activity views.
You may inject fragments in classes annotated with @EActivity, @EFragment, @EView, @EViewGroup, @EBean, using @FragmentById or @FragmentByTag. If you don't specify any value on the annotation, the field name is used.
We recommend using
@FragmentByIdrather then@FragmentByTag, because no compile time validation is performed for the latter.
Please be aware that @FragmentById and @FragmentByTag can only inject fragments, not create them, so they must already exist in the activity (either by defining them in the layout or by creating them programmatically in onCreate().
You can inject fragments even if they are not annotated with
@EFragment.
@EActivity(R.layout.fragments)
public class MyFragmentActivity extends FragmentActivity {
@FragmentById
MyFragment myFragment;
@FragmentById(R.id.myFragment)
MyFragment myFragment2;
@FragmentByTag
MyFragment myFragmentTag;
@FragmentByTag("myFragmentTag")
MyFragment myFragmentTag2;
}14/06/2012 The 2.6 release is out
- Get started!
- Cookbook, full of recipes
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow