Instead, with a single line, RoboGuice will take care of injecting that view into your activity:
12345678
publicclassLoginActivityextendsRoboActivity{@InjectView(R.id.sign_in_button)ButtonmSignInButtonView;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){// I have a reference to mSignInButtonView here.}}
Now we’re rewriting ashoka-survey-mobile as a layered native app).
We have a LoginView (POJO) which needs references to views present on-screen. Normally, we would instantiate the POJO in the activity, and pass it all the views it needs.
But can we do this with RoboGuice? We can’t really use @InjectView in a POJO. It needs an Activity context.
The next best thing is to inject the activity into the POJO (RoboGuice is smart enough to inject the correct activity), and pick the views manually using findViewById.
So in the activity:
12345678
publicclassLoginActivityextendsRoboActivity{@InjectLoginViewloginView;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){loginView.onCreate();// Need to manually build up the view references inside LoginView}}
and in LoginView:
12345678910
publicclassLoginView{@InjectActivityactivity;// This gets injected with the correct instance of LoginActivityprivateButtonbuttonView;privateEditTexteditText;publicvoidonCreate(){buttonView=activity.findViewById(R.id.my_button);editText=activity.findViewById(R.id.my_edit_text);}}
And then your POJO can manipulate the views that are on-screen as required.