Application module
に保存された値を変更して、新しいアクティビティまたは別の場所に戻すことはできますか?ApplicationModuleを更新し、各注入時に更新された値を取得することができます
@Module
public class ApplicationModule {
private HandsFreeApp app;
private Property currentProperty;
public ApplicationModule(HandsFreeApp app) {
this.app = app;
Log.d("myTag", "ApplicationModule: "+hashCode());
}
@Provides @Singleton Context provideAppContext() {
return app;
}
@Provides @Singleton RestClient provideRestClient(Realm realm) {
TokenDataStore tokenDataStore = new TokenDataStore(realm);
GetTokenUseCase tokenUseCase = new GetTokenUseCase(new TokenRepository(tokenDataStore));
return new RestClientRetrofit(tokenUseCase);
}
@Provides @Singleton Realm provideRealm() {
Realm.setDefaultConfiguration(new RealmConfiguration.Builder(app).build());
return Realm.getDefaultInstance();
}
@Provides @Singleton Property getCurrentProperty(Realm realm) {
if(currentProperty == null) {
BookingStore store = new BookingLocalStore(realm);
currentProperty = store.getPropertyList().toBlocking().first().get(0);
}
Log.d("myTag", "getCurrentProperty: "+currentProperty.getName());
return currentProperty;
}
public void setCurrentProperty(Property currentProperty) {
this.currentProperty = currentProperty;
Log.d("myTag", "setCurrentProperty: "+this.currentProperty.getName());
}}
しかしgetCurrentProperty()
が呼び出され、一度だけ
+1。このような場合、シングルトンの振る舞いが必要な場合は、@ Singletonバインディング(おそらく@Qualifier)をリダイレクトするか、[シングルトンの動作を保証するために他の技術を使う]必要があります(https://google.github.io /guava/releases/19.0/api/docs/com/google/common/base/Suppliers.html#memoize(com.google.common.base.Supplier))。 –