Site icon C1CTech

Android Dagger2 Example Using Retrofit

<p>Here in this tutorial i will explain you what the dependency injection is and why do we need it&quest; I will also let you know why do we use it and what happens when we don&&num;8217&semi;t use it&quest;We will also learn about how we inject dependency in our android project using dagger2 &period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"What-is-Dependency-Injection">What is Dependency Injection&quest;<&sol;span><&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>The term &OpenCurlyDoubleQuote;Dependency Injection” or in short &OpenCurlyDoubleQuote;DI” is  a design pattern &comma;which is build upon the concept of Inversion of Control&period; <&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li><strong>That means a class should get its dependencies from outside&period; <&sol;strong><&sol;li>&NewLine;<li><strong>In more simpler words&comma; we can say class cannot instantiate another class using a new keyword inside it&period; Instead&comma; you have to supply the object from outside&period;<&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>To understand it in more detail&comma; let’s break the term Dependency Injection in two parts Dependency and Injection&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;333399&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidDagger2Example"><strong>Here<&sol;strong><&sol;a>&period;<&sol;span><&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"0nbf5l35lxE" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;0nbf5l35lxE" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;0nbf5l35lxE&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;0nbf5l35lxE&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<h4><strong><span id&equals;"What-is-Dependency" style&equals;"color&colon; &num;000080&semi;">What is Dependency&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>Consider the following Java class&period;<&sol;p>&NewLine;<pre><code>public class Customer &lbrace;&NewLine; &NewLine;Private Account account&semi;&NewLine;public Customer&lpar;&rpar;&NewLine;&lbrace;&NewLine; account &equals; new Account&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>In the above class&comma; we are creating the object of class <span style&equals;"color&colon; &num;008000&semi;"><strong>Account<&sol;strong><&sol;span> inside the class <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span>&period; So here&comma; we can say that the <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span> is dependent on <span style&equals;"color&colon; &num;008000&semi;"><strong>Account <&sol;strong><&sol;span>object or we can say <span style&equals;"color&colon; &num;008000&semi;"><strong>Account<&sol;strong><&sol;span> is a dependency for the class <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<h4><strong><span id&equals;"What-is-Injection" style&equals;"color&colon; &num;000080&semi;">What is Injection&quest;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>As we seen the <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span> is Dependent on <span style&equals;"color&colon; &num;008000&semi;"><strong>Account<&sol;strong><&sol;span>&comma; now how do we get this <span style&equals;"color&colon; &num;008000&semi;"><strong>Account<&sol;strong><&sol;span> object inside the <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span> class &period;<&sol;p>&NewLine;<p>We can consider the following ways&period;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Create Account object inside the Customer Constructor or inside the Customer class&period; <&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>public class Customer &NewLine;&lbrace; &NewLine;Private Account account&semi;&NewLine;public Customer&lpar;&rpar; &NewLine;&lbrace; &NewLine;account &equals; new Account&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>Or<&sol;p>&NewLine;<pre><code>public class Customer &NewLine;&lbrace; &NewLine;Private Account account&semi;&NewLine;&NewLine;account &equals; new Account&lpar;&rpar;&semi; &NewLine; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>But this is a bad idea because now <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span> class get dependent on <span style&equals;"color&colon; &num;008000&semi;"><strong>Account<&sol;strong><&sol;span> object &period;<&sol;p>&NewLine;<p>Ideally java class should be as independent from other classes&comma; this increases the possibility of &&num;8212&semi;&&num;8211&semi;<&sol;p>&NewLine;<ol>&NewLine;<li>reusing these classes&period;<&sol;li>&NewLine;<li>Testing these classes independently from other classes&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Instead of using new to instantiate the Account inside Customer&&num;8217&semi;s constructor we can pass the Account as an argument to the Customer&&num;8217&semi;s constructor&period;<&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code>public class Customer &lbrace; &NewLine;&NewLine;private Account account&semi;&NewLine;public Customer&lpar;Account account&rpar;&NewLine; &lbrace; &NewLine;this&period;account &equals; account&semi; &NewLine; &rcub; &NewLine; &rcub;<&sol;code><&sol;pre>&NewLine;<p>Doing this makes the class <span style&equals;"color&colon; &num;008000&semi;"><strong>Customer<&sol;strong><&sol;span> now independent from class <strong><span style&equals;"color&colon; &num;008000&semi;">Account <&sol;span><&sol;strong>&period;So this is what we call as <strong><span style&equals;"color&colon; &num;0000ff&semi;">Dependency Injection<&sol;span><&sol;strong>&period; Means supplying the dependencies&lpar;object&rpar; from outside the class instead of creating the object inside the class using new keyword&period;<&sol;p>&NewLine;<h3><strong><span id&equals;"What-is-Dagger-2" style&equals;"color&colon; &num;000080&semi;">What is Dagger 2&quest;<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>Dagger is a fully static&comma; compile-time dependency injection framework for both Java and Android&period; It is an adaptation of an earlier version created by Square and now maintained by Google&period;<&sol;strong><&sol;p>&NewLine;<h3><strong><span id&equals;"Working-with-Dagger-2" style&equals;"color&colon; &num;000080&semi;">Understanding Basics of Dagger2<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Before moving ahead in our Android Project&comma; let’s understand first the basics of Dagger 2&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>So we have three major things in Dagger&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<h5><span id&equals;"1-Dependency-Provider" style&equals;"color&colon; &num;000080&semi;"> Dependency Provider<&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Dependencies are the objects that we need to instantiate inside a class&period; And we learned before that we cannot instantiate a class inside a class&period; Instead&comma; we need to supply it from outside&period; So the class which will provide us the objects that are called <strong><span style&equals;"color&colon; &num;008000&semi;">dependencies<&sol;span><&sol;strong> is called <span style&equals;"color&colon; &num;008000&semi;"><strong>Dependency Provider<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>And in <span style&equals;"color&colon; &num;008000&semi;"><strong>dagger2<&sol;strong><&sol;span> the class that you want to make a Dependency Provider&comma; you need to annotate it with the <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Module<&sol;strong> <&sol;span>annotation&period;<&sol;li>&NewLine;<li>And inside the class&comma; you will create methods that will provide the objects &lpar;or dependencies&rpar;&period; With dagger2 we need to annotate these methods with the <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Provides<&sol;strong><&sol;span> annotation&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">&commat;Module and &commat;Provides<&sol;span><&sol;strong>&colon; define classes and methods which provide dependencies<&sol;li>&NewLine;<&sol;ul>&NewLine;<h5><strong><span id&equals;"2-Dependency-Consumer" style&equals;"color&colon; &num;000080&semi;"> Dependency Consumer<&sol;span><&sol;strong><&sol;h5>&NewLine;<ul>&NewLine;<li>dependency consumer is a class where we need to instantiate the objects&period; But now we don’t need to instantiate it with the new keyword &lpar;I am assuming java here&rpar;&period; We do not even need to get it as an argument&period; But dagger will provide the dependency&comma; and for this&comma; we just need to annotate the object declaration with <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Inject&period;<&sol;strong><&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<h5><span id&equals;"3-Component"> <span style&equals;"color&colon; &num;000080&semi;">Component<&sol;span><&sol;span><&sol;h5>&NewLine;<ul>&NewLine;<li>Finally&comma; we need some connection between our dependency provider and dependency consumer&period;<br &sol;>&NewLine;For this&comma; we will create an interface by annotating it with <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Component<&sol;strong><&sol;span>&period;  And rest of the thing will be done by Dagger&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Using Dagger2 in android project<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>In this tutorial my aim is to understand you&comma; how to use <strong><span style&equals;"color&colon; &num;008000&semi;">Dagger2<&sol;span><&sol;strong> in our project instead of <span style&equals;"color&colon; &num;008000&semi;"><strong>working with retrofit <&sol;strong><&sol;span>&period;<a href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-retrofit-example&sol;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Android retrofit Example<&sol;strong><&sol;span><&sol;a> is my previous post Which i am going to modify here so that i can focus on <strong><span style&equals;"color&colon; &num;008000&semi;">dagger2<&sol;span><&sol;strong>&period;You can get source code of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Android retrofit Example<&sol;strong><&sol;span> from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;RetrofitExample"><span style&equals;"color&colon; &num;00ff00&semi;"><strong>here<&sol;strong><&sol;span><&sol;a>&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Firstly go to <strong><span style&equals;"color&colon; &num;008000&semi;">build&period;gradle<&sol;span><&sol;strong>&lpar;app level&rpar; and add the dependency for dagger2 as shown below and <strong><span style&equals;"color&colon; &num;008000&semi;">sync<&sol;span><&sol;strong> the project&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre><code><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;dagger2<&sol;span><&sol;strong>&NewLine;compile 'com&period;google&period;dagger&colon;dagger&colon;2&period;13'&NewLine;annotationProcessor 'com&period;google&period;dagger&colon;dagger-compiler&colon;2&period;13'<&sol;code><&sol;pre>&NewLine;<h3><strong><span id&equals;"Creating-Modules" style&equals;"color&colon; &num;000080&semi;">Creating Modules<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>For this project&comma; we need two modules&period; The first one is the<strong> <span style&equals;"color&colon; &num;008000&semi;">AppModule<&sol;span><&sol;strong>&comma; and the Next one is <strong><span style&equals;"color&colon; &num;008000&semi;">ApiClientModule<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"App-Module">App Module<&sol;span><&sol;strong><&sol;span><&sol;p>&NewLine;<p>This module will provide the Context&period; You already know that we need Context everywhere&comma; and in Retrofit as well we need the context object&period; And as the DI rule says we need an outsider to supply the objects&comma; so here we will create this module that will give us the Context&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Create a new class named <span style&equals;"color&colon; &num;008000&semi;"><strong>AppModule<&sol;strong><&sol;span> and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">AppModule&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androiddagger2example&period;module&semi;&NewLine;&NewLine;import android&period;app&period;Application&semi;&NewLine;&NewLine;import javax&period;inject&period;Singleton&semi;&NewLine;&NewLine;import dagger&period;Module&semi;&NewLine;import dagger&period;Provides&semi;&NewLine;&NewLine;&commat;Module&NewLine;public class AppModule &lbrace;&NewLine;&NewLine; private Application mApplication&semi;&NewLine;&NewLine; public AppModule&lpar;Application mApplication&rpar; &lbrace;&NewLine; this&period;mApplication &equals; mApplication&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Provides&NewLine; &commat;Singleton&NewLine; Application getApplication&lpar;&rpar; &lbrace;&NewLine; return mApplication&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>The above code is straightforward&comma; and we just use a new annotation <span style&equals;"color&colon; &num;008000&semi;"><strong>&commat;Singleton<&sol;strong><&sol;span>&period; In dagger&comma; we have this annotation&comma; when we want a Single object&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span id&equals;"API-Module" style&equals;"color&colon; &num;000080&semi;">ApiClient Module<&sol;span><&sol;strong><&sol;p>&NewLine;<p>This module will provide us retrofit object which we will use later in our MainActivity to make request&period;<&sol;p>&NewLine;<ul>&NewLine;<li> So&comma; create the class named <span style&equals;"color&colon; &num;008000&semi;"><strong>AppClientModule<&sol;strong><&sol;span> and write the following code&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">ApiClientModule&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androiddagger2example&period;module&semi;&NewLine;&NewLine;import javax&period;inject&period;Singleton&semi;&NewLine;&NewLine;import dagger&period;Module&semi;&NewLine;import dagger&period;Provides&semi;&NewLine;import retrofit2&period;Retrofit&semi;&NewLine;import retrofit2&period;converter&period;gson&period;GsonConverterFactory&semi;&NewLine;&NewLine;&commat;Module&NewLine;public class ApiClientModule &lbrace;&NewLine;&NewLine; public final String BASE&lowbar;URL&semi;&NewLine;&NewLine; public ApiClientModule&lpar;String url&rpar; &lbrace;&NewLine; this&period;BASE&lowbar;URL &equals; url&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Provides&NewLine; &commat;Singleton&NewLine; Retrofit getClient&lpar;&rpar; &lbrace;&NewLine; return new Retrofit&period;Builder&lpar;&rpar;&NewLine; &period;baseUrl&lpar;BASE&lowbar;URL&rpar;&NewLine; &period;addConverterFactory&lpar;GsonConverterFactory&period;create&lpar;&rpar;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>We have done with the Modules&period; Now let’s define the Component and then we will inject the objects&period;<&sol;p>&NewLine;<h3><strong><span id&equals;"Building-Component" style&equals;"color&colon; &num;000080&semi;">Building Component<&sol;span><&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li>Now&comma; we will create the Component&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">ApiComponent&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androiddagger2example&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;module&period;ApiClientModule&semi;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;module&period;AppModule&semi;&NewLine;&NewLine;import javax&period;inject&period;Singleton&semi;&NewLine;&NewLine;import dagger&period;Component&semi;&NewLine;&NewLine;&commat;Singleton&NewLine;&commat;Component&lpar;modules &equals; &lbrace;ApiClientModule&period;class&comma; AppModule&period;class&rcub;&rpar;&NewLine;public interface ApiComponent &lbrace;&NewLine;&NewLine; void inject&lpar;MainActivity mainActivity&rpar;&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Now create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>MyApplication<&sol;strong><&sol;span>&period; In this class we will build the <strong><span style&equals;"color&colon; &num;008000&semi;">ApiComponent<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MyApplication&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androiddagger2example&semi;&NewLine;&NewLine;import android&period;app&period;Application&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;module&period;ApiClientModule&semi;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;module&period;AppModule&semi;&NewLine;&NewLine;public class MyApplication extends Application &lbrace;&NewLine;&NewLine; private ApiComponent mApiComponent&semi;&NewLine;&NewLine; &commat;Override&NewLine; public void onCreate&lpar;&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;&rpar;&semi;&NewLine;&NewLine; mApiComponent &equals; DaggerApiComponent&period;builder&lpar;&rpar;&NewLine; &period;appModule&lpar;new AppModule&lpar;this&rpar;&rpar;&NewLine; &period;apiClientModule&lpar;new ApiClientModule&lpar;"http&colon;&sol;&sol;cricapi&period;com&sol;"&rpar;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public ApiComponent getComponent&lpar;&rpar; &lbrace;&NewLine; return mApiComponent&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;ff0000&semi;"><strong>Before moving ahead Rebuild your project&period; <&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li>So&comma; we have our API component&comma; but we need to instantiate this class when our application launches&period; And for this&comma; we need to define it inside our App Manifest file&period; So open your <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> and modify it as shown below&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><img class&equals;"wp-image-720 size-full aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;dagger&period;png" alt&equals;"" width&equals;"835" height&equals;"478" &sol;><&sol;p>&NewLine;<h3><strong><span id&equals;"Injecting-Dependency-with-Dagger-2" style&equals;"color&colon; &num;000080&semi;">Injecting Dependency with Dagger 2<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Now finally&comma; we can inject the dependency&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Come inside <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and modify it as below&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Mainactivity&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre><code>package com&period;c1ctech&period;androiddagger2example&semi;&NewLine;&NewLine;import android&period;app&period;Application&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DefaultItemAnimator&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DividerItemDecoration&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;adapter&period;MatchCalenderAdapter&semi;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;model&period;CalenderResponse&semi;&NewLine;import com&period;c1ctech&period;androiddagger2example&period;model&period;MatchCalender&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;import javax&period;inject&period;Inject&semi;&NewLine;&NewLine;import retrofit2&period;Call&semi;&NewLine;import retrofit2&period;Callback&semi;&NewLine;import retrofit2&period;Response&semi;&NewLine;import retrofit2&period;Retrofit&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;injecting retrofit and application dependency<&sol;span><&sol;strong>&NewLine; &commat;Inject&NewLine; Retrofit retrofit&semi;&NewLine;&NewLine; &commat;Inject&NewLine; Application application&semi;&NewLine;&NewLine; private final static String API&lowbar;KEY &equals; "ENTER YOUR API KEY"&semi;&NewLine; private static final String TAG &equals; MainActivity&period;class&period;getSimpleName&lpar;&rpar;&semi;&NewLine; RecyclerView recyclerView&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; &lpar;&lpar;MyApplication&rpar; getApplication&lpar;&rpar;&rpar;&period;getComponent&lpar;&rpar;&period;inject&lpar;this&rpar;&semi;&NewLine;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;match&lowbar;calender&lowbar;recycler&lowbar;view&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;this&rpar;&rpar;&semi;&NewLine; recyclerView&period;addItemDecoration&lpar;new DividerItemDecoration&lpar;this&comma; LinearLayoutManager&period;VERTICAL&rpar;&rpar;&semi;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Using retrofit<&sol;strong> <&sol;span>&NewLine; ApiInterface apiService &equals; retrofit&period;create&lpar;ApiInterface&period;class&rpar;&semi;&NewLine;&NewLine; Call&lt&semi;CalenderResponse&gt&semi; call &equals; apiService&period;getMatchCalender&lpar;API&lowbar;KEY&rpar;&semi;&NewLine; call&period;enqueue&lpar;new Callback&lt&semi;CalenderResponse&gt&semi;&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onResponse&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Response&lt&semi;CalenderResponse&gt&semi; response&rpar; &lbrace;&NewLine; int statuscode &equals; response&period;code&lpar;&rpar;&semi;&NewLine; List&lt&semi;MatchCalender&gt&semi; matchlist &equals; response&period;body&lpar;&rpar;&period;getData&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Using application<&sol;span><&sol;strong>&NewLine; recyclerView&period;setAdapter&lpar;new MatchCalenderAdapter&lpar;matchlist&comma; application&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onFailure&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Throwable t&rpar; &lbrace;&NewLine; Log&period;e&lpar;TAG&comma; t&period;toString&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>Writing the below line allow the <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity<&sol;strong><&sol;span> to inject or use any of the dependencies of all the modules defined in <strong><span style&equals;"color&colon; &num;008000&semi;">ApiComponent<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<pre><code>&lpar;&lpar;MyApplication&rpar; getApplication&lpar;&rpar;&rpar;&period;getComponent&lpar;&rpar;&period;inject&lpar;this&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>When you run your application it will look like this&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1554" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1580979777&period;png" alt&equals;"Screenshot&lowbar;1580979777" width&equals;"488" height&equals;"868" &sol;><&sol;p>&NewLine;<p>It will show the list of matches that will be occur today or later&period;<&sol;p>&NewLine;<p>I hope that this tutorial will help you in understanding the dagger2&period;Thank you&period;&NewLine;

Exit mobile version