Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

AndroidAnnotations 與 RoboGuice 整合實務

編輯:關於 Android 程式設計

RoboGuice 把 Google Guice 的依賴注入能力帶進 Android,而 AndroidAnnotations 則以編譯期注入聞名。兩者整合後,可以在不繼承 RoboActivity 的情況下,使用 @RoboGuice 指定事件監聽器。

整合重點

先在專案加入 AndroidAnnotations 與 RoboGuice,再於 Activity 上使用 @EActivity 與 @RoboGuice。過去需要繼承 RoboActivity,現在只要標註即可。

優缺點

RoboGuice 的優勢是依賴注入與事件觀察機制;AndroidAnnotations 的優勢是編譯期處理、效能負擔小,並支援 @Click、@Background、@UiThread 等標註。

在 @EBean 中使用

RoboGuice 主要支援 Activity。若要在 @EBean 中使用 Guice 注入,可透過 Application 取得 Injector,在 @AfterInject 方法中手動注入成員。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved