Fix Developer options page memory leak

Change-Id: I12d53c41adee56850cd294ae89e7a56328639558
Fixes: 117583710
Test: manual
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.development
This commit is contained in:
Stanley Wang
2018-10-29 17:04:53 +08:00
parent 152de8abde
commit f838aa1059
3 changed files with 62 additions and 6 deletions

View File

@@ -470,7 +470,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new DefaultLaunchPreferenceController(context, "density"));
controllers.add(new DefaultLaunchPreferenceController(context, "background_check"));
controllers.add(new DefaultLaunchPreferenceController(context, "inactive_apps"));
controllers.add(new AutofillLoggingLevelPreferenceController(context));
controllers.add(new AutofillLoggingLevelPreferenceController(context, lifecycle));
controllers.add(new AutofillResetOptionsPreferenceController(context));
return controllers;
}

View File

@@ -27,11 +27,15 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnDestroy;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
public final class AutofillLoggingLevelPreferenceController
extends DeveloperOptionsPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
LifecycleObserver, OnDestroy {
private static final String TAG = "AutofillLoggingLevelPreferenceController";
private static final String AUTOFILL_LOGGING_LEVEL_KEY = "autofill_logging_level";
@@ -40,7 +44,7 @@ public final class AutofillLoggingLevelPreferenceController
private final String[] mListSummaries;
private final AutofillDeveloperSettingsObserver mObserver;
public AutofillLoggingLevelPreferenceController(Context context) {
public AutofillLoggingLevelPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
Resources resources = context.getResources();
@@ -48,7 +52,15 @@ public final class AutofillLoggingLevelPreferenceController
mListSummaries = resources.getStringArray(R.array.autofill_logging_level_entries);
mObserver = new AutofillDeveloperSettingsObserver(mContext, () -> updateOptions());
mObserver.register();
// TODO: there should be a hook on AbstractPreferenceController where we could unregister it
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public void onDestroy() {
mObserver.unregister();
}
@Override