Search code examples
androidguiceroboguice

InjectView in an Activity read as a parameter


I've a helper class with one function, that receives an activity as a parameter. I want to inject a view from that activity. Is this possible?

public class myClass {
   public void myMethod(final Activity activity) {
      // Use here a TextView from that activity - how to inject it?

Thanks in advance!


Solution

  • Inject it as a field of the class. You can't inject directly into a method body.

    public class myClass {
        @InjectView(R.id.myTextView)
        private TextView myTextView;
        public void myMethod(final Activity activity) {
            // Use here a TextView from that activity - how to inject it?
            myTextView.setText("something");
        }
    }