AndroidAnnotations provide those good things and even more for less than 50kb, without any runtime perf impact! Let's take a look at the following code:@EActivity(R.layout.translate) // Sets content view to R.layout.translatepublic class TranslateActivity extends Activity { @ViewById // Injects R.id.textInput EditText textInput; @ViewById(R.id.myTextView) // Injects R.id.myTextView TextView result; @AnimationRes // Injects android.R.anim.fade_in Animation fadeIn; @Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); } @Background // Executed in a background thread void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui thread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); } // [...]}
// ================================================ ==================================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. (a subclass is generated)
For instance, the following class:
package com.some.company;@EActivitypublic class MyActivity extends Activity { // ...}
Will generate the following subclass, in the same package but in another source folder :( generate a subclass of MyActivity)
package com.some.company;public final class MyActivity_ extends MyActivity { // ...}
This subclass adds behavior to your activity by overriding some methods (for instanceonCreate()
), Yet delegating the cballs to super.
That is the reason why you must add_
To your activity names inAndroidManifest.xml
(The subclass generated in the configuration file)
Starting an annotated activityIn Android, you usually start an activity this way:
startActivity(this, MyListActivity.class);
However, with AndroidAnnotations, the real activity that must be started isMyListActivity_
:
startActivity(this, MyListActivity_.class);
Intent Builder
Since AndroidAnnotations 2.4
We provide a static helper to let you start the generated 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();
Since AndroidAnnotations 2.7
You can also usestartActivityForResult()
Equivalent:
MyListActivity_.intent(context).startForResult();
Starting an annotated ServiceIn Android, you usually start a service this way:
startService(this, MyService.class);
However, with AndroidAnnotations, the real Service that must be started isMyService_
:
startService(this, MyService_.class);
Intent Builder
Since AndroidAnnotations 2.7
We provide a static helper to let you start the generated 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(
AvailableAnnotationsEnhanced components
- @ EActivity
- @ EApplication
- @ EBean
- @ EFragment
- @ EProvider
- @ EReceiver
- @ EIntentService
- @ EService
- @ EView
- @ EViewGroupInjection
- @ AfterExtras
- @ AfterInject
- @ AfterViews
- @ App
- @ Bean
- @ Extra
- @ FragmentArg
- @ FragmentById
- @ FragmentByTag
- @ FromHtml
- @ HttpsClient
- @ NonConfigurationInstance
- @ RootContext
- @ SystemService
- @ ViewById
- @ ViewsByIdEvent binding
- @ TextChange
- @ AfterTextChange
- @ BeforeTextChange
- @ Editexception
- @ FocusChange
- @ CheckedChange
- @ Touch
- @ Click
- @ LongClick
- @ ItemClick
- @ ItemLongClick
- @ ItemSelect
- @ OptionsItem
- @ SeekBarProgressChange
- @ SeekBarTouchStart
- @ SeekBarTouchStopThreading
- @ Background
- @ UiThread
- @ SupposeBackground
- @ SupposeUiThreadMisc
- @ InstanceState
- @ WindowFeature
- @ Fullscreen
- @ NoTitle
- @ CustomTitle
- @ OptionsMenu
- @ OptionsMenuItem
- @ OrmLiteDao
- @ RoboGuice
- @ Trace
- @ Transactional
- @ OnActivityResult
- @ OnActivityResult. Extra
- @ HierarchyViewerSupport
- @ ServiceAction
- @ Receiver
- @ ReceiverAction
- @ ReceiverAction. Extra
- @ IgnoredWhenDetached
- @ WakeLockResource injection
- @ StringRes
- @ AnimationRes
- @ ColorRes
- @ DimensionPixelOffsetRes
- @ DimensionPixelSizeRes
- @ DimensionRes
- @ BooleanRes
- @ ColorStateListRes
- @ DrawableRes
- @ IntArrayRes
- @ IntegerRes
- @ LayoutRes
- @ MovieRes
- @ StringArrayRes
- @ TextArrayRes
- @ TextRes
- @ HtmlResRest API
- @ Rest
- @ RestService
- @ Get
- @ Post
- @ Put
- @ Delete
- @ Options
- @ Head
- @ Accept
- @ RequiresHeader
- @ RequiresCookie
- @ RequiresCookieInUrl
- @ RequiresAuthentication
- @ SetsCookie
- @ RequiresCookieInUrlTypesafe SharedPreferences
- @ Defaboolean Boolean
- @ Defaflofloat
- @ DefaultInt
- @ DefaultLong
- @ DefaultString
- @ Defastrstringset
- @ DefaultRes
- @ Pref
- @ SharedPref: This is where you can use it to view its documents in detail.