For example, let's say that my vacation adapter created with Retrofit lives inside the Application class. I would like to get it inside an Activity, so I will write the following code:
public class MainActivity extends Activity { @Inject MyRestAdapter mRestAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ((GlobalApplication) getApplication()).getComponent().inject(this); } }
Of course, he will do his job. But ... How does this differ from calling the getApplication () method, and then explicitly pulls MyRestAdapter into MainActivity? Yes, Dagger 2 will simplify the setup by automatically bringing everything to Activity, but you still need to explicitly indicate where you need these dependencies from, and that, if I understand correctly, the whole DI goal wins. Can I say that Dagger 2 is a βsemi-automatic service locatorβ, or is it just the tutorials that misled me, and is there a proper way to embed dependencies with Dagger 2 in a view or activity from an application?
"DI DI", , , . , , Activity Android, , , , , , , . , Activity ( TextView [0]), DI , .
Activity
TextView
" " . , , -DI . ( ).
, , , , , - . , , . MainActivity AnalyticsLogger - ? , , , .
MainActivity
AnalyticsLogger
[0], Paint
Paint
, . , , , Android. AndroidInjection.inject(this) onCreate. " ". , Dagger / , , , , (, ) . , Android - / .
AndroidInjection.inject(this)
onCreate
Source: https://habr.com/ru/post/1626986/More articles:Angular 2: a problem with custom components - angularjsHow to clear an existing digit in excel using matlab? - graphhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1626983/react-redux-pass-thisprops-to-actions-from-component&usg=ALkJrhg4TiQbpxTDPt7DeY-wFJV_CkpDnAJava: How can I make a ball behave like a bouncing and resilient ball? - javaopen the application using the http: // links in ios - ios"No subscription found for Azure Account" - azureWhat are responsible users in factory differentials? - phabricatorHow to find a list comprehension in python code - pythonCritical CSS with SASS - htmlJava threads are lazy versus fusion versus short circuit - javaAll Articles