Enhance Fragments - PerfectCarl/androidannotations GitHub Wiki

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;
	
	@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
	UserDao userDao;

	@Click
	void myButton() {
	}

	@UiThread
	void uiThread() {

	}

	@AfterInject
	void calledAfterInjection() {
	}
	
	@AfterViews
	void calledAfterViewInjection() {
	}

	@Receiver(actions = "org.androidannotations.ACTION_1")
	protected void onAction1() {

	}
}

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.

Fragment Layout

The standard way to associate a view with a fragment is to override onCreateView():

@EFragment
public class MyFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
        return view;
    }
}

You can let AndroidAnnotations handle that for you by setting the value param of the @EFragment annotation:

@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}

If you need to override onCreateView(), e.g. because you need to access savedInstanceState, you can still let AndroidAnnotations handle the layout creation by returning null:

@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }
}

Injecting Fragments

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 @FragmentById rather 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;
}