Giter Site home page Giter Site logo

simple-dagger-2's Introduction

SimpleDagger

A simple dagger 2 integration app.

The base flow of this project is to store some user's name and phone number which is available in the model class and print them. This Model.java will be acting as an dependent to be injected in the activity.

  • Step One:

    Add respective gradle dependencies(Check for versions when you add).

    implementation 'com.google.dagger:dagger:2.16'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.16'
    
  • Then create Modules:

    Create the ApplicationModule.java and annotate with @Module. This class will be the projects main module, which will be providing the application's context and the application's instance. @Provides annotation is used for providing instances inside a module.

    Your ApplicationModule.java will be something like this

     @Module
     public class ApplicationModule {
    
         private MyApplication mApplication;
    
         public ApplicationModule(MyApplication mApplication) {
             this.mApplication = mApplication;
         }
    
         @Provides
         @Singleton
         public Application provideApplication() {
             return mApplication;
         }
    
         @Provides
         @Singleton
         public Context provideContext() {
             return mApplication;
         }
     }

    Same as ApplicationModule create ActivityModule.java Module This class provides the required entities for an activity. Here we are in need of a model, so provide the model here.

    And ActivityModule.java class will be like this

     @Module
     public class ActivityModule {
    
       @Provides
       public Model provideModel() {
         return new Model();
       }
    
     }
  • Now go for creating Components:

    The main job of the components is to connect modules with the activity. So this should be an interface class.

    Create an ApplicationComponent.java interface. Annotate with @Singleton, because this will be our application's main instance to be travelled untill our app dies. Then declare the respective module with this component annotation.

    The ApplicationComponent will be like this

     @Singleton
     @Component(modules = ApplicationModule.class)
     public interface ApplicationComponent {
    
         void inject(MyApplication application);
    
         Context getContext();
    
         Application getApplication();
     }

    Now create ActivityComponent.java interface. This should be annotated with some custom scope, because this is our sub module and which is similar like sub-singleton. So it has to be annotated with custom scope. I have used @ActivityScope here.

    Your ActivityComponent will be like this

     @ActivityScope
     @Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
     public interface ActivityComponent {
    
         void inject(DaggerActivity activity);
    
     }

    ApplicationComponent.class is the main dependencies, so it is declared as dependencies, and the ActivityModule is declared as modules

  • Now lets start injecting stuffs:

    Before building stuffs dont forget to rebuild your project. Only then you'll get your dagger generated codes

    In your application class initialize your ApplicationModule.

     ApplicationComponent mComponent = DaggerApplicationComponent
           .builder()
           .applicationModule(new ApplicationModule(this))
           .build();

    In your Activity class initialize your ActivityModule.

     ActivityComponent mComponent = DaggerActivityComponent
             .builder()
             .applicationComponent(((MyApplication) getApplication()).getComponent())
             .activityModule(new ActivityModule())
             .build();
     mComponent.inject(this);

    By injecting your Model class, those stuffs can be used.

      @Inject Model model;

simple-dagger-2's People

Contributors

android-rajaganapathi avatar

Watchers

James Cloos avatar Ramkumar Velmurugan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.