AndroidAnnotations user manual-how does AndroidAnnotations work?
AndroidAnnotations works in a simple way. It uses a standard java injection tool and automatically adds an additional compilation step to generate source code.
What is the source code? Every enhanced class, such as every Activity injected with @ EActivity, will automatically generate a subclass of this Activity with the class name + underline as the class name.
For example, the following class:
package com.some.company;@EActivitypublic class MyActivity extends Activity { // ...}
The following sub-classes are generated. They are in different folders under the same package:
package com.some.company;public final class MyActivity_ extends MyActivity { // ...}
This subclass adds some actions to your activity by writing several methods (such as onCreate.
The above is why you need to add an underscore to the end of your class name when you perform AndroidManifest. xml lifecycle Acitivty:
Start an Activity that uses injection:
In Android, we usually start an activity in the following way:
startActivity(this, MyListActivity.class);
However, if AndroidAnnotations is used, the actually started activity is MyListActivity _ instead of MyListActivity:
startActivity(this, MyListActivity_.class);
Intent Builder (AndroidAnnotations 2.4 and later ):
We provide a static help class to start the edited activity:
// Starting the activityMyListActivity_.intent(context).start();// Building an intent from the activityIntent intent = MyListActivity_.intent(context).get();// You can provide flagsMyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();// You can even provide extras defined with @Extra in the activityMyListActivity_.intent(context).myDateExtra(someDate).start();
In AndroidAnnotations 2.7 and later versions, you can use another method to start Activity startActivityForResult:
MyListActivity_.intent(context).startForResult();
Start a service using annotation:
In Android, we usually start a service in the following way:
startService(this, MyService.class);
However, if AndroidAnnotations is used, the actually started Service is MyService _ instead of MyService:
startService(this, MyService_.class);
Intent Builder (AndroidAnnotations 2.7 or later ):
We provide a static help class to start the production Service:
// Starting the serviceMyService_.intent(context).start();// Building an intent from the activityIntent intent = MyService_.intent(context).build();// You can provide flagsMyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start();