活动拦截器

Ale*_*lov 4 android guice roboguice

android中是否有任何方法可以拦截活动方法调用(只是标准的调用,比如"onStart.onCreate")?我有很多的功能,必须存在于我的应用程序的每一次活动,和(因为它使用不同类型的活动(列表,首选项))这样做是创建我的自定义扩展的每个活动类的唯一途径,这糟透了:(

PS我使用roboguice,但由于Dalvik不支持运行时的代码生成,我想它没有多大帮助.

PSS我考虑过使用AspectJ,但这太麻烦了,因为它需要很多复杂功能(ant的build.xml和所有垃圾)

abo*_*bss 5

roboguice 1.1.1版本包含对注入上下文的组件的一些基本事件支持.有关详细信息,请参阅http://code.google.com/p/roboguice/wiki/Events.

例如:

@ContextScoped
public class MyObserver {
  void handleOnCreate(@Observes OnCreatedEvent e) {
    Log.i("MyTag", "onCreated");
  }
}

public class MyActivity extends RoboActivity {
  @Inject MyObserver observer;  // injecting the component here will cause auto-wiring of the handleOnCreate method in the component.

  protected void onCreate(Bundle state) {
    super.onCreate(state); /* observer.handleOnCreate() will be invoked here */
  }
}
Run Code Online (Sandbox Code Playgroud)