I have a set of @Singleton
and @Provides
method in my module class for the purpose of creating Singleton instance throughout the application. Everything works fine except few bottle neck scenarios like as follows:
STEP 1. I am creating a Retrofit instance from OKHttpClient
with Auth token in it to make a authenticated api calls each time (Auth token retrieval and insertion is handled through SharedPreferences
). But the problem starts at the time of relaunching the activity after when i logout the application by clearing databases and shared preferences values.
STEP 2. After logout, am making an another request to fetch auth tokens and inserting into SharedPreferences
again for future use.
STEP 3: Now if i proceed with the rest of api calls, the previous instance of the Dagger @Singleton
and @Provides
method remains same unless and until if i relaunch the app by clearing it from the recent task. (New auth token is not updated)
Fixes Needed:
How to trigger the Dagger provider methods forcibly to trigger or revoke it again?
Is there any method to refresh the application class data as similar behaviour like when the app relaunches.?
Please find my Dagger 2 architecture used in my project:
NetworkModule.java (Dagger Module class)
@Module
public class NetworkModule {
private Context context;
public NetworkModule(Application app) {
this.context = app;
}
@Provides
@Named("network.context")
Context providesContext() {
return context;
}
@Singleton
@Provides
OkHttpClient providesOkHttpClient(@Named("network.context")final Context context) {
final UserProfile userProfile = GsonUtils.createPojo(SharedPrefsUtils.getString(Constants.SHARED_PREFS.USERS_PROFILE, "",context), UserProfile.class);
Logger.i(userProfile != null && !TextUtils.isEmpty(userProfile.getAuth_token()) ? userProfile.getAuth_token() : "----OAuth token empty---");
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.header("Api-Version", "application/vnd.addo-v1+json")
.header("Access-Token", userProfile != null && !TextUtils.isEmpty(userProfile.getAuth_token()) ? userProfile.getAuth_token() : "")
.header("App-Version", Utils.getVersionName(context))
.header("Device-Platform","android")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
return httpClient.build();
}
@Provides
@Named(Constants.INJECTION.BASE_URL)
String providebaseURL() {
return Constants.URL.BASE_URL;
}
@Singleton
@Provides
Retrofit providesRetrofit(@Named("network.context")Context context, @Named(Constants.INJECTION.BASE_URL) String baseURL, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
@Singleton
@Provides
NetworkApiService providesNetworkApiService(Retrofit retrofit){
return retrofit.create(NetworkApiService.class);
}
@Singleton
@Provides
ProjectPresenter providesProjectPresenter(NetworkApiService networkApiService){
return new ProjectPresenterImpl(networkApiService);
}
}
AppComponent.java (Dagger component class)
@Singleton
@Component(modules = {NetworkModule.class})
public interface AppComponent {
//ACtivity
void inject(AuthenticationActivity authenticationActivity);
void inject(MainActivity mainActivity);
//Fragments
void inject(ProjectsListFragment projectsListFragment);
}
Application.java (Class used to create Dagger component)
public class Application extends Application {
private AppComponent appComponent ;
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
ButterKnife.setDebug(BuildConfig.DEBUG);
appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).networkModule(new NetworkModule(this)).build();
}
public AppComponent getAppComponent() {
return appComponent;
}
}
Kindly help me with your suggestions or tips to resolve this weird behaviour of Dagger 2. Any kind of solutions will be much helpful to me since I am completely stuck up with this for the past 6 days. I am clueless and perplexed because my complete architecture is built on top of this. Please forgive me for typos and corrections. Ping me if there are any clarifications required regarding the same. Thanks in advance.