Я хотел бы знать, какой контекст вводит Roboguice, это контекст приложения или текущая активность?
Я пытаюсь использовать как Roboguice, так и Robospice. Я внедряю SpiceManager
Робоспайса во фрагмент, но фрагмент не знает о SpiceManager
, он видит его через интерфейс, скажем, MyInterface
.
public class MyFragment extends RoboFragment {
//this is where the SpiceManager gets injected
@Inject MyInterface manager;
...
}
//this is the implementation that I'm going to inject
//it is simultaneously an event listener for the fragment's life cycle events so that the
//SpiceManager can be appropriately started and stopped.
public class MyManager implements MyInterface {
private SpiceManager spiceManager = new SpiceManager(MySpiceService.class);
//Which context will get injected here? How can I make Roboguice inject a specific context that I want, for example, a specific activity that I want.
private @Inject Context context;
//Here, I need to start the SpiceManager
public void myFragmentOnStart(@Observes OnStartEvent onStart) {
//SpiceManager requires a context, more specifically an activity which will be destroyed and then garbage collected, so It shouldn't be an application context because the resources SpiceManager uses will never be released.
spiceManager.start(context);
}
public void myFragmentOnStop(@Observes OnStopEvent onStop){
if (spiceManager.isStarted()) {
spiceManager.shouldStop();
}
}
}
Мои вопросы:
Может ли RoboGuice отслеживать события фрагментов помимо событий Activity, документация не ясна?
Правильно ли я думаю, что SpiceManager нужен контекст, который будет уничтожен при уничтожении фрагмента/активности? Я просмотрел код SpiceManager.start(Context context)
, и он создает WeakReference
для переданного Context
.
Как заставить RoboGuice вводить определенные Context/Activity
?
Возможно ли сделать это, не зная MyFragment
, что объект MyInterface
, который он использует, нуждается в Context
?
Кстати, я узнал, что у OnStopEvent
есть метод getActivity()
, поэтому нет проблем с получением Activity
в onStop
, но OnStartEvent
— это просто пустой класс.