AndroidAnnotation
目录(?)[+]
- How It Works
- Overview 概述
- Starting an annotated activity 启动一个注解过的activity
- Intent Builder Intent生成器
- Starting an annotated Service 启动一个注解过的Service
- Intent Builder Intent生成器
- Is there any performance impact这会产生什么性能影响吗
How It Works
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.
AndroidAnnotations使用起来很简单。它自动添加一个额外的编译步骤,生成相应的、使用标准
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.
这是怎样的代码?对于每个被优化的class,比如每个加了
@EActivity
注解的activity,会生成一个子类,它和父类的名字一样,只是在末尾加了一个下划线
_
。
For instance, the following class:
以下面的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.
子类通过覆盖一些方法为你的activity增加行为(比如
onCreate()
),然后委托这些调用给父类。
That is the reason why you must add
_
to your activity names in
AndroidManifest.xml
:
这就是为什么你必须在
AndroidManifest.xml
中为你的activity名添加
_
的原因:
<activity android:name=".MyListActivity_" />
Starting an annotated activity 启动一个注解过的activity
In Android, you usually start an activity this way:
在Android中,你一般这样启动一个activity:
startActivity(this, MyListActivity.class);
However, with
AndroidAnnotations
, the real activity that must be started is
MyListActivity_
:
但是通过
AndroidAnnotations
,这个真正被启动的activity必须是
MyListActivity_
:
startActivity(this, MyListActivity_.class);
Intent Builder Intent生成器
Since AndroidAnnotations 2.4
We provide a static helper to let you start the generated activity:
我们提供了一个静态方法,让你启动这个生成的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:
同理,你也可以使用
startActivityForResult()
:
MyListActivity_.intent(context).startForResult();
Starting an annotated Service 启动一个注解过的Service
In Android, you usually start a service this way:
在Android中,你通常这样启动一个service:
startService(this, MyService.class);
However, with
AndroidAnnotations
, the real Service that must be started is
MyService_
:
但是通过
AndroidAnnotations
,这个真正被启动的service必须是
MyService_
:
startService(this, MyService_.class);
Intent Builder Intent生成器
Since AndroidAnnotations 2.7
We provide a static helper to let you start the generated service:
我们提供了一个静态方法,让你启动这个生成的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
.
简单的回答是
no
。想了解更多请参看
FAQ。
Now that you get the basics, let's see how to enhance Activities.
现在你已经了解了基础知识,让我们看看如何 enhance Activities。