Browse Source

Destroy references to solve leak

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
0d53e244e2
  1. 1
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 18
      app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java
  3. 7
      app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java

1
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -248,6 +248,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
Log.e(TAG, "onDestroy: ", e);
}
unbindActivityCheckerService();
RetrofitFactory.getInstance().destroy();
}
@Override

18
app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.java

@ -25,10 +25,11 @@ public final class RetrofitFactory {
private static RetrofitFactory instance;
private final MainActivity mainActivity;
private final int cacheSize = 10 * 1024 * 1024; // 10 MB
private final Cache cache = new Cache(new File(Utils.cacheDir), cacheSize);
private IgErrorsInterceptor igErrorsInterceptor;
private MainActivity mainActivity;
private Retrofit.Builder builder;
private Retrofit retrofit;
private Retrofit retrofitWeb;
@ -56,6 +57,7 @@ public final class RetrofitFactory {
private Retrofit.Builder getRetrofitBuilder() {
if (builder == null) {
igErrorsInterceptor = new IgErrorsInterceptor(mainActivity);
final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.followRedirects(false)
.followSslRedirects(false)
@ -64,7 +66,7 @@ public final class RetrofitFactory {
// clientBuilder.addInterceptor(new LoggingInterceptor());
}
clientBuilder.addInterceptor(new AddCookiesInterceptor())
.addInterceptor(new IgErrorsInterceptor(mainActivity));
.addInterceptor(igErrorsInterceptor);
final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Caption.class, new Caption.CaptionDeserializer())
@ -95,4 +97,16 @@ public final class RetrofitFactory {
}
return retrofitWeb;
}
public void destroy() {
if (igErrorsInterceptor != null) {
igErrorsInterceptor.destroy();
}
igErrorsInterceptor = null;
mainActivity = null;
retrofit = null;
retrofitWeb = null;
builder = null;
instance = null;
}
}

7
app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java

@ -22,7 +22,7 @@ import okhttp3.ResponseBody;
public class IgErrorsInterceptor implements Interceptor {
private static final String TAG = IgErrorsInterceptor.class.getSimpleName();
private final MainActivity mainActivity;
private MainActivity mainActivity;
public IgErrorsInterceptor(@NonNull final MainActivity mainActivity) {
this.mainActivity = mainActivity;
@ -94,6 +94,7 @@ public class IgErrorsInterceptor implements Interceptor {
}
private void showErrorDialog(@StringRes final int messageResId) {
if (mainActivity == null) return;
if (messageResId == 0) return;
final ConfirmDialogFragment dialogFragment = ConfirmDialogFragment.newInstance(
Constants.GLOBAL_NETWORK_ERROR_DIALOG_REQUEST_CODE,
@ -105,4 +106,8 @@ public class IgErrorsInterceptor implements Interceptor {
);
dialogFragment.show(mainActivity.getSupportFragmentManager(), "network_error_dialog");
}
public void destroy() {
mainActivity = null;
}
}
Loading…
Cancel
Save