1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 13:26:59 +00:00

Destroy references to solve leak

This commit is contained in:
Ammar Githam 2021-03-22 23:46:47 +09:00
parent 91e13a23ad
commit 0d53e244e2
3 changed files with 23 additions and 3 deletions

View File

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

View File

@ -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;
}
}

View File

@ -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;
}
}