Fixed calls to update() when configuration changed.

update() is an expensive operation and should only be called
once under normal circumstances (it still needs to be called due to external changes). In particular, it should not be called again on orientation changes.

The first approach to solve the orientation change caused the volume title to not be shown when the screen changed on multi window environments; the fix for title issue caused the update to be called again on configuration changes.

This change properly fixes both issues by removing the onAttach() / onDetach() methods and using a clearer variable (mNeedsUpdate) to avoid future regressions.

BUG: 24508289
BUG: 27989238
Change-Id: I140f5a541cda293f1c476d3b80a5bc8918e18b08
This commit is contained in:
Felipe Leme
2016-05-12 14:37:11 -07:00
parent 1590b8c005
commit 6ebd561960

View File

@@ -117,7 +117,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
private Preference mExplore;
private boolean mDetached;
private boolean mNeedsUpdate;
private boolean isVolumeValid() {
return (mVolume != null) && (mVolume.getType() == VolumeInfo.TYPE_PRIVATE)
@@ -164,18 +164,22 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
mExplore = buildAction(R.string.storage_menu_explore);
mDetached = false;
mNeedsUpdate = true;
setHasOptionsMenu(true);
}
private void setTitle() {
getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume));
}
private void update() {
if (!isVolumeValid()) {
getActivity().finish();
return;
}
getActivity().setTitle(mStorageManager.getBestVolumeDescription(mVolume));
setTitle();
// Valid options may have changed
getFragmentManager().invalidateOptionsMenu();
@@ -238,6 +242,7 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
mSummary.setPercent((int) ((usedBytes * 100) / totalBytes));
mMeasure.forceMeasure();
mNeedsUpdate = false;
}
private void addPreference(PreferenceGroup group, Preference pref) {
@@ -314,8 +319,10 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
mStorageManager.registerListener(mStorageListener);
if (!mDetached) {
if (mNeedsUpdate) {
update();
} else {
setTitle();
}
}
@@ -325,25 +332,12 @@ public class PrivateVolumeSettings extends SettingsPreferenceFragment {
mStorageManager.unregisterListener(mStorageListener);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mDetached = false;
}
@Override
public void onDetach() {
super.onDetach();
mDetached = true;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMeasure != null) {
mMeasure.onDestroy();
}
mDetached = false;
}
@Override