简体   繁体   中英

Dagger dependency injection

Actually i am new to dependency injection and dagger, i have been writing boiler plate code all these time and am trying to learn dagger

I have a global class to save preference values

@Module(injects = AppPrefes.class)
public class AppPrefes {

    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;

    public AppPrefes(Context context, String Preferncename) {
        this.appSharedPrefs = context.getSharedPreferences(Preferncename,
                Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }

    /****
     * 
     * getdata() get the value from the preference
     * 
     * */
    @Provides
    public String getData(String key) {
        return appSharedPrefs.getString(key, "");
    }    

    /****
     * 
     * SaveData() save the value to the preference
     * 
     * */
    @Provides
    public void SaveData(String Tag, String text) {
        prefsEditor.putString(Tag, text);
        prefsEditor.commit();
    } 
}

How could i possibly inject this class in My activity In my activity oncreate i have put like this

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ObjectGraph objectGraph = ObjectGraph.create();
        AppPrefes app = objectGraph.get(AppPrefes.class);   
    }

but how should i pass dependency ie the context and the preference name to AppPrefes class,i am completely new to dependency injection please correct me if i am wrong Since dagger documentation seems to be little tough for me am asking this question.

I believe if you are willing to take the risk of creating a static variable to store the instance of the application,

public enum ApplicationHolder
{
    INSTANCE;

    private MyApplication application;

    public MyApplication getApplication()
    {
        return application;
    }

    public void setApplication(MyApplication application)
    {
        this.application = application;
    }

}

public MyApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        ApplicationHolder.INSTANCE.setApplication(this);
        objectGraph.create(new RootModule());
    }
}

Afterwards, you can create a Dagger module which can provide the Context of the Application instance.

@Module(complete = false, library = true)
public class ContextModule
{
    @Provides
    public Context providesContext()
    {
        return ApplicationHolder.INSTANCE.getApplication().getApplicationContext();
    }
}

@Module(
    includes =
    {
        ContextModule.class
    },
    injects =
    {
        AppPrefes.class
    }
)
public class RootModule
{
}

Then you can inject this in your Activity with @Inject , however you need to provide the String you want to inject as a constructor parameter like this: https://stackoverflow.com/a/18105271/2413303

The structure of modules and stuff for me were like here: How to set up DAGGER dependency injection from scratch in Android project?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM