Skip to content
pyricau edited this page May 28, 2012 · 7 revisions

Support for FragmentActivity

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 {

}

Fragment Support

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.

Enhanced Fragments

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 @EBean injected inside fragments: they have access to the activity views.

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Clone this wiki locally