首页 文章

使用Dagger 2的Singleton SharedPreferences

提问于
浏览
0

我正在尝试使用Dagger 2创建一个Singleton of SharedPreferences,并且我不断收到以下错误消息:

com.test.app.injection.component.ConfigPersistentComponent作用于@ com.test.app.injection.ConfigPersistent可能不会引用具有不同范围的绑定:@Singleton class com.test.app.data.local.PreferencesHelper

我试图通过我的MainPresenter中的Injected构造函数访问共享首选项 . 使用Singleton DataManager进行API访问可以通过Injected构造函数正常工作,但不能用于SharedPreferences .

这是我的设置:

的AppModule

@Module(includes = {ApiModule.class})
public class AppModule {
    private final Application application;

    public AppModule(Application application) {
        this.application = application;
    }

    @Provides
    Application provideApplication() {
        return application;
    }

    @Provides
    @ApplicationContext
    Context provideContext() {
        return application;
    }

    @Provides
    @Singleton
    @ApplicationContext
    SharedPreferences provideSharedPreference(@ApplicationContext Context context) {
        return context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    }
}

AppComponent

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    @ApplicationContext
    Context context();

    Application application();

    DataManager apiManager();

    @ApplicationContext
    SharedPreferences prefManager(Context context);
}

ConfigPersistentComponent

/**
 * A dagger component that will live during the lifecycle of an Activity or Fragment but it won't be
 * destroy during configuration changes. Check {@link BaseActivity} and {@link BaseFragment} to see
 * how this components survives configuration changes. Use the {@link ConfigPersistent} scope to
 * annotate dependencies that need to survive configuration changes (for example Presenters).
 */
@ConfigPersistent
@Component(dependencies = AppComponent.class)
public interface ConfigPersistentComponent {

    ActivityComponent activityComponent(ActivityModule activityModule);

    FragmentComponent fragmentComponent(FragmentModule fragmentModule);
}

的ConfigPersistent

/**
 * A scoping annotation to permit dependencies conform to the life of the {@link
 * ConfigPersistentComponent}
 */
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigPersistent {
}

PreferencesHelper

@Singleton
public class PreferencesHelper {

    private final SharedPreferences preferences;

    @Inject
    PreferencesHelper(SharedPreferences sharedPreferences) {
        preferences = sharedPreferences;
    }

    public void putString(@Nonnull String key, @Nonnull String value) {
        preferences.edit().putString(key, value).apply();
    }

    public String getString(@Nonnull String key) {
        return preferences.getString(key, "");
    }

    public void putBoolean(@Nonnull String key, @Nonnull boolean value) {
        preferences.edit().putBoolean(key, value).apply();
    }

    public boolean getBoolean(@Nonnull String key) {
        return preferences.getBoolean(key, false);
    }

    public void putInt(@Nonnull String key, @Nonnull boolean value) {
        preferences.edit().putBoolean(key, value).apply();
    }

    public int getInt(@Nonnull String key) {
        return preferences.getInt(key, -1);
    }

    public void clear() {
        preferences.edit().clear().apply();
    }
}

MainPresenter

@ConfigPersistent
public class MainPresenter extends BasePresenter<MainMvpView> {

    private final PreferencesHelper preferencesHelper;
    private final DataManager dataManager;

    @Inject
    public MainPresenter(PreferencesHelper preferencesHelper, DataManager dataManager) {
        this.preferencesHelper = preferencesHelper;
        this.dataManager = dataManager;
    }
...
}

1 回答

  • 1
    @Provides
    @Singleton
    // @ApplicationContext // <-- remove this
    SharedPreferences provideSharedPreference(@ApplicationContext Context context) {
        return context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    }
    

    并添加这个:

    @Singleton
    @Component(modules = AppModule.class)
    public interface AppComponent {    
        ...
    
    
        // @ApplicationContext
        SharedPreferences prefManager();
    
        PreferencesHelper preferencesHelper();
    }
    

相关问题