Search code examples
androidandroid-annotations

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


My activity uses an adapter that needs a context

@EActivity
public class MyActivity extends Activity {

    @Bean
    MyAdapter adapter;

}

The adapter in itself:

@EBean
public class MyAdapter extends Adapter {

    @RootContext
    Context context;

}

Whn I open the generated activity, I see:

adapter = MyAdapter_.getInstance_(getActivity());

The Activity context leaks memory. I wanna change it to Application Context I could do that using a setter, I suppose, but I gotta time it somehow, so can I make AndroidAnnotations pass Application Context instead?


Solution

  • You cannot pass the app Context with @RootContext, but you can inject the application object with @App:

    @EApplication
    public class MyApp extends Application {
      ...
    }
    // do not forget to register MyApp_ in the manifest
    
    @App
    MyApp myApp;
    

    Then you can use that as the app Context.