HowItWorks - WonderCsabo/androidannotations GitHub Wiki
Overview
AndroidAnnotations works in a very simple way. It automatically adds an extra compilation step that generates source code, using the standard Java Annotation Processing Tool.
What source code ? For each enhanced class, for example each @EActivity
annotated activity, a subclass of this activity is generated, with the same name plus an underscore appended at the end.
For instance, the following class:
package com.some.company;
@EActivity
public class MyActivity extends Activity {
// ...
}
Will generate the following subclass, in the same package but in another source folder:
package com.some.company;
public final class MyActivity_ extends MyActivity {
// ...
}
This subclass adds behavior to your activity by overriding some methods (for instance onCreate()
), yet delegating the calls to super.
That is the reason why you must add _
to your activity names in AndroidManifest.xml
:
<activity android:name=".MyListActivity_" />
Starting an annotated activity
In Android, you usually start an activity this way:
startActivity(this, MyListActivity.class);
However, with AndroidAnnotations, the real activity that must be started is MyListActivity_
:
startActivity(this, MyListActivity_.class);
Intent Builder
Since AndroidAnnotations 2.4
We provide a static helper to let you start the generated activity:
// Starting the activity
MyListActivity_.intent(context).start();
// Building an intent from the activity
Intent intent = MyListActivity_.intent(context).get();
// You can provide flags
MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();
// You can even provide extras defined with @Extra in the activity
MyListActivity_.intent(context).myDateExtra(someDate).start();
Since AndroidAnnotations 2.7
You can also use the startActivityForResult()
equivalent:
MyListActivity_.intent(context).startForResult(REQUEST_CODE);
Note that you can use the @OnActivityResult annotation to get result code and extra value.
@OnActivityResult(REQUEST_CODE)
void onResult(int resultCode) {
}
Since AndroidAnnotations 3.3
You can easily pass options Bundle
parameter using the intent builder:
MyListActivity_.intent(context).withOptions(bundle).start();
Since AndroidAnnotations 4.0.0
Activity [transition animations](http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)) can be added using the fluent intent builder.
MyListActivity_.intent(context).start().withAnimation(enterAnimRes, exitAnimRes));
Starting an annotated Service
In Android, you usually start a service this way:
startService(this, MyService.class);
However, with AndroidAnnotations, the real Service that must be started is MyService_
:
startService(this, MyService_.class);
Intent Builder
Since AndroidAnnotations 2.7
We provide a static helper to let you start the generated service:
// Starting the service
MyService_.intent(context).start();
// Building an intent from the activity
Intent intent = MyService_.intent(context).build();
// You can provide flags
MyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start();
Is there any performance impact?
The short answer is no. More on this subject in the FAQ.
Now that you get the basics, let's see how to enhance Activities.