如何在 AndroidAnnotations 驱动的适配器中传递应用程序而不是 Activity 上下文?

How to pass Application instead of Activity context in an AndroidAnnotations-powered adapter?

我的 activity 使用需要上下文的适配器

@EActivity
public class MyActivity extends Activity {

    @Bean
    MyAdapter adapter;

}

适配器本身:

@EBean
public class MyAdapter extends Adapter {

    @RootContext
    Context context;

}

当我打开生成的 activity 时,我看到:

adapter = MyAdapter_.getInstance_(getActivity());

Activity 上下文泄漏内存。我想将其更改为应用程序上下文 我想我可以使用 setter 来做到这一点,但我必须以某种方式计时,所以我可以让 AndroidAnnotations 传递应用程序上下文吗?

您不能使用 @RootContext 传递应用程序 Context,但您可以使用 @App:

注入应用程序对象
@EApplication
public class MyApp extends Application {
  ...
}
// do not forget to register MyApp_ in the manifest

@App
MyApp myApp;

然后您可以将其用作应用 Context