<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bipin Alee Magar on Medium]]></title>
        <description><![CDATA[Stories by Bipin Alee Magar on Medium]]></description>
        <link>https://medium.com/@bipinAle?source=rss-5068b27e6a76------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jOh4pnossqHwUdFpaPGFyw.jpeg</url>
            <title>Stories by Bipin Alee Magar on Medium</title>
            <link>https://medium.com/@bipinAle?source=rss-5068b27e6a76------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:04:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bipinAle/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dependency Injection(Dagger 2 android)]]></title>
            <link>https://medium.com/@bipinAle/dagger-2-android-simplest-example-7ba0c2bfc702?source=rss-5068b27e6a76------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ba0c2bfc702</guid>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Bipin Alee Magar]]></dc:creator>
            <pubDate>Thu, 30 Mar 2017 12:30:15 GMT</pubDate>
            <atom:updated>2018-02-17T15:59:43.123Z</atom:updated>
            <content:encoded><![CDATA[<p>Dependency injection is the process of preparing necessary services/Objects which will be further used. And this process is handled with the help of third party tools like Dagger 2. So dagger is just a simple tool which creates necessary services and circulate throughout the application. And prepared services can be further used without creating a new instances.</p><p>In order to understand dagger you should have three answers of these questions:W<strong><em>hich(services),Where(client) and How</em></strong> a dagger should provide object.</p><p>Here in this example I am using</p><p>a.which — -&gt;&gt; service(e.g <em>Sharedpreference,Database etc)</em></p><p>b.where — -&gt;&gt;Activity/Fragments (are the client which uses the service)</p><p>c.how — -&gt;&gt; Creating method inside your module class with a @Provides annotation that returns an object of the sharedpreference.</p><ol><li>build.gradle(app level)</li></ol><pre>annotationProcessor <strong>&quot;com.google.dagger:dagger-compiler:2.8&quot;<br></strong>apt <strong>&#39;com.google.dagger:dagger-compiler:2.8&#39;<br></strong>compile <strong>&quot;com.google.dagger:dagger:2.8&quot;<br></strong>provided <strong>&#39;javax.annotation:jsr250-api:1.0&#39;<br></strong>compile <strong>&#39;javax.inject:javax.inject:1&#39;</strong></pre><p>2. build.gradle(project level)</p><pre>classpath <strong>&#39;com.neenbedankt.gradle.plugins:android-apt:1.8&#39;</strong></pre><p>Once done sync with gradle.</p><p>3. Which????</p><p>Ans: I want Shared Preference to be served by dagger.</p><p>So I create a class that acts as provider of Shared Preference.</p><p><strong><em>StorageModule.java</em></strong></p><pre><em>//provider class for shared pref<br></em><strong>@Module<br>public class </strong>StorageModule {<br><br>    <strong>private </strong>Activity <strong>activity</strong>;<br><br>    <strong>public </strong>StorageModule(Activity activity) {<br>        <strong>this</strong>.<strong>activity </strong>= activity;<br>    }<br><br>    <strong>@Singleton<br>    @Provides<br>    </strong>SharedPreferences provideSharedPrefences() {<br>        <strong>return </strong>PreferenceManager.<em>getDefaultSharedPreferences</em>(<strong>activity</strong>);<br>    }<br><br>}</pre><p>4. Create a interface <strong><em>ActivityComponent</em></strong> which acts as <strong><em>bridge</em></strong> between provider(module class ) and consumer(activity).</p><pre><em>//mediator for provider(storage Module) and consumer<br></em><strong>@Singleton<br>@Component</strong>(modules = StorageModule.<strong>class</strong>)<em>//provider<br></em><strong>public interface </strong>ActivityComponent {<br>    <strong>void </strong>inject(MainActivity mainActivity);<em>//consumer<br><br></em>}</pre><p>5. Now in our Activity we can use the object of SharedPreference using <em>@Injec</em>t annotation, without creating a new object.</p><p>In order to inject this object we have to make a ActivityComponent and use it to inject in our required place. We have to write <em>Dagger+ourComponent</em> manually. Module name with lower camel case.</p><pre><strong>public </strong>ActivityComponent getComponent() {<br>    <strong>return </strong>DaggerActivityComponent.<em>builder</em>().<br>            storageModule(<strong>new </strong>StorageModule(<strong>this</strong>)).build();<br>}</pre><p>and use it to inject in activity using</p><pre>getComponent().inject(<strong>this</strong>);</pre><p>6. Now, MainActivity.java look like this:</p><pre><strong>public class </strong>MainActivity <strong>extends </strong>AppCompatActivity {<br><br>    <strong>@Inject<br>    </strong>SharedPreferences <strong>sharedPreferences</strong>;<br><br>    <strong>public </strong>ActivityComponent getComponent() {<br>        <strong>return </strong>DaggerActivityComponent.<em>builder</em>().<br>                storageModule(<strong>new </strong>StorageModule(<strong>this</strong>)).build();<br>    }<br><br>    <strong>@Override<br>    protected void </strong>onCreate(Bundle savedInstanceState) {<br>        <strong>super</strong>.onCreate(savedInstanceState);<br>        setContentView(R.layout.<strong><em>activity_main</em></strong>);<br>        getComponent().inject(<strong>this</strong>);//call the inject method of component class.<br><br>        <em>//to write in sharedpref<br>         </em>SharedPreferences.Editor prefEditor =        <strong>sharedPreferences</strong>.edit();<br>        prefEditor.putString(<strong>&quot;UserName&quot;</strong>, <strong>&quot;John Doe&quot;</strong>);<br>        prefEditor.putInt(<strong>&quot;UserAge&quot;</strong>, 22);<br>        prefEditor.apply();<br><br>        <em>//to read from sharedPref<br>        </em>String name = <strong>sharedPreferences</strong>.getString(<strong>&quot;UserName&quot;</strong>, <strong>&quot;DEFAULT&quot;</strong>);<br>        <strong>int </strong>age = <strong>sharedPreferences</strong>.getInt(<strong>&quot;UserAge&quot;</strong>, 0);<br>        Toast.<em>makeText</em>(<strong>this</strong>, <strong>&quot;name  &quot; </strong>+ name + <strong>&quot;age  &quot; </strong>+ age, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();<br><br><br>    }<br>}</pre><p>Thats all.. hope it helps you.</p><p>Please feel free to comment if you have any queries.</p><p>Thanks!!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ba0c2bfc702" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>