AndroidAnnotations——How It Works,AndroidAnnotation是如何工作的

翻译 2013年12月04日 15:22:05

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:
public class MyActivity extends Activity {
  // ...

Will generate the following subclass, in the same package but in another source folder:
public final class MyActivity_ extends MyActivity {
  // ...
This subclass adds behavior to your activity by overriding some methods (for instanceonCreate()), yet delegating the calls to super.

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:
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:
// Starting the activity

// Building an intent from the activity
Intent intent = MyListActivity_.intent(context).get();

// You can provide flags

// You can even provide extras defined with @Extra in the activity

Since AndroidAnnotations 2.7

You can also use the startActivityForResult() equivalent:
同理,你也可以使用 startActivityForResult() 

Starting an annotated Service 启动一个注解过的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_:
但是通过 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:
// Starting the service

// Building an intent from the activity
Intent intent = MyService_.intent(context).build();

// You can provide flags

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

Selenium Grid How it Works

To understand Selenium Grid you first need to understand how the standard Selenium web testing tool ...

Inside Qt Series (十五):Qt/e 输入法,How it works?

前面我们介绍了Qte输入法的基本设计思路,以及一个最简单的例子,那么,Qte的输入法是如何工作的呢?本节我们就来看一下Qte的源代码,一起来解开这个谜团。 在Qte的Client/Server体系结...
  • ruglcc
  • ruglcc
  • 2012年09月10日 14:39
  • 1226

What is Index and how it works in SQL Server?

What is Index and how it works in SQL Server?

OpenCV+VS, tell you how it really works

How to build applications with OpenCV inside the Microsoft Visual Studio Everything I describe he...

Garbage Collection in .NET - How it really works

Garbage collection is a process of releasing the memory used by the objects, which are no longer ref...

How It All Works: The Messy Implementation Details(转)

How It All Works: The Messy Implementation Details Classes Used by an Event Source The IConnection...

How VSync works, and why people loathe it

I recently learned that how I thought vsync worked was wrong, and now knowing the way it really does...

浏览器是如何工作的 - How Browser works

原文: 有点长,前面介绍的比较细,但是到后面就越写越糊了,两年了  都没有继续写下去,据说是没...

浏览器是如何工作的 - How Browser works(二)

解析 Parsing-general 既然解析是渲染引擎中一个非常重要的过程,我们将稍微深入的研究它。首先简要介绍一下解析。 解析一个文档即将其转换为具有一定意义的结构——编码可以理解和使...

浏览器是如何工作的 - How Browser works(四)

布局 Layout 当渲染对象被创建并添加到树中,它们并没有位置和大小,计算这些值的过程称为 layout 或 reflow。 Html使用基于流的布局模型,意味着大部分时间,可以...
您举报文章:AndroidAnnotations——How It Works,AndroidAnnotation是如何工作的