1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-27 02:07:12 +00:00

Clear activity views

This commit is contained in:
M66B 2022-04-15 21:29:00 +02:00
parent 1b0d80cc9b
commit 224fcdb2f4
4 changed files with 34 additions and 28 deletions

View file

@ -20,7 +20,6 @@ package eu.faircode.email;
*/ */
import android.Manifest; import android.Manifest;
import android.animation.Animator;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -60,14 +59,11 @@ import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.recyclerview.selection.SelectionTracker;
import androidx.recyclerview.widget.RecyclerView;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -807,26 +803,7 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
@Override @Override
public void onFragmentViewDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) { public void onFragmentViewDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
log(fm, f, "onFragmentViewDestroyed"); log(fm, f, "onFragmentViewDestroyed");
try { Helper.clearViews(f);
for (Field field : f.getClass().getDeclaredFields()) {
Class<?> type = field.getType();
if (View.class.isAssignableFrom(type) ||
Animator.class.isAssignableFrom(type) ||
SelectionTracker.class.isAssignableFrom(type) ||
SelectionTracker.SelectionPredicate.class.isAssignableFrom(type) ||
RecyclerView.Adapter.class.isAssignableFrom(type)) {
Log.i("Clearing " + f.getClass().getSimpleName() + ":" + field.getName());
field.setAccessible(true);
if (Animator.class.isAssignableFrom(type))
((Animator) field.get(f)).setTarget(null);
field.set(f, null);
}
}
} catch (Throwable ex) {
Log.w(ex);
}
} }
@Override @Override

View file

@ -1047,10 +1047,6 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override @Override
protected void onDestroy() { protected void onDestroy() {
if (lastSnackbar != null) {
lastSnackbar.setAction(null, null);
lastSnackbar = null;
}
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.unregisterReceiver(creceiver); lbm.unregisterReceiver(creceiver);
super.onDestroy(); super.onDestroy();

View file

@ -766,6 +766,8 @@ public class ApplicationEx extends Application
@Override @Override
public void onActivityDestroyed(@NonNull Activity activity) { public void onActivityDestroyed(@NonNull Activity activity) {
log(activity, "onActivityDestroyed"); log(activity, "onActivityDestroyed");
Helper.clearViews(activity);
} }
@Override @Override

View file

@ -23,6 +23,7 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION; import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
import android.Manifest; import android.Manifest;
import android.animation.Animator;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.app.Activity; import android.app.Activity;
@ -108,6 +109,7 @@ import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent; import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.recyclerview.selection.SelectionTracker;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
@ -1270,6 +1272,35 @@ public class Helper {
return fragment.getClass().getName() + ":result:" + who; return fragment.getClass().getName() + ":result:" + who;
} }
static void clearViews(Object instance) {
try {
for (Field field : instance.getClass().getDeclaredFields()) {
Class<?> type = field.getType();
if (View.class.isAssignableFrom(type) ||
Animator.class.isAssignableFrom(type) ||
Snackbar.class.isAssignableFrom(type) ||
SelectionTracker.class.isAssignableFrom(type) ||
SelectionTracker.SelectionPredicate.class.isAssignableFrom(type) ||
RecyclerView.Adapter.class.isAssignableFrom(type)) {
Log.i("Clearing " + instance.getClass().getSimpleName() + ":" + field.getName());
field.setAccessible(true);
if (Animator.class.isAssignableFrom(type))
((Animator) field.get(instance)).setTarget(null);
if (Snackbar.class.isAssignableFrom(type)) {
((Snackbar) field.get(instance)).setAction(null, null);
}
field.set(instance, null);
}
}
} catch (Throwable ex) {
Log.w(ex);
}
}
// Graphics // Graphics
static int dp2pixels(Context context, int dp) { static int dp2pixels(Context context, int dp) {