Add Preference#onDetachedFromActivity

Change-Id: I7c78f96d41b33b902e54fe1c0b7246d555aab0ed
This commit is contained in:
Jason Monk
2015-08-11 16:40:44 -04:00
parent 012d014c43
commit e18dc50c63
5 changed files with 39 additions and 9 deletions

View File

@@ -23885,6 +23885,7 @@ package android.preference {
method protected void onClick();
method protected android.view.View onCreateView(android.view.ViewGroup);
method public void onDependencyChanged(android.preference.Preference, boolean);
method protected void onDetachedFromActivity();
method protected java.lang.Object onGetDefaultValue(android.content.res.TypedArray, int);
method public void onParentChanged(android.preference.Preference, boolean);
method protected void onPrepareForRemoval();

View File

@@ -25841,6 +25841,7 @@ package android.preference {
method protected void onClick();
method protected android.view.View onCreateView(android.view.ViewGroup);
method public void onDependencyChanged(android.preference.Preference, boolean);
method protected void onDetachedFromActivity();
method protected java.lang.Object onGetDefaultValue(android.content.res.TypedArray, int);
method public void onParentChanged(android.preference.Preference, boolean);
method protected void onPrepareForRemoval();

View File

@@ -1184,9 +1184,10 @@ public class Preference implements Comparable<Preference> {
/**
* Called when the Preference hierarchy has been attached to the
* {@link PreferenceActivity}. This can also be called when this
* Preference has been attached to a group that was already attached
* to the {@link PreferenceActivity}.
* {@link PreferenceActivity} or {@link PreferenceFragment}. This can
* also be called when this Preference has been attached to a group
* that was already attached to the {@link PreferenceActivity} or
* {@link PreferenceFragment}.
*/
protected void onAttachedToActivity() {
// At this point, the hierarchy that this preference is in is connected
@@ -1194,6 +1195,16 @@ public class Preference implements Comparable<Preference> {
registerDependency();
}
/**
* Called when the Preference hierarchy has been detached from the
* {@link PreferenceActivity} or {@link PreferenceFragment}. This can
* also be called when this Preference has been removed from a group
* that was already attached to the {@link PreferenceActivity} or
* {@link PreferenceFragment}.
*/
protected void onDetachedFromActivity() {
}
private void registerDependency() {
if (TextUtils.isEmpty(mDependencyKey)) return;

View File

@@ -187,7 +187,11 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla
private boolean removePreferenceInt(Preference preference) {
synchronized(this) {
preference.onPrepareForRemoval();
return mPreferenceList.remove(preference);
boolean success = mPreferenceList.remove(preference);
if (mAttachedToActivity) {
preference.onDetachedFromActivity();
}
return success;
}
}
@@ -263,7 +267,7 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla
protected boolean isOnSameScreenAsChildren() {
return true;
}
@Override
protected void onAttachedToActivity() {
super.onAttachedToActivity();
@@ -280,11 +284,17 @@ public abstract class PreferenceGroup extends Preference implements GenericInfla
}
@Override
protected void onPrepareForRemoval() {
super.onPrepareForRemoval();
protected void onDetachedFromActivity() {
super.onDetachedFromActivity();
// We won't be attached to the activity anymore
mAttachedToActivity = false;
// Dispatch to all contained preferences
final int preferenceCount = getPreferenceCount();
for (int i = 0; i < preferenceCount; i++) {
getPreference(i).onDetachedFromActivity();
}
}
@Override

View File

@@ -396,6 +396,9 @@ public class PreferenceManager {
*/
boolean setPreferences(PreferenceScreen preferenceScreen) {
if (preferenceScreen != mPreferenceScreen) {
if (mPreferenceScreen != null) {
mPreferenceScreen.onDetachedFromActivity();
}
mPreferenceScreen = preferenceScreen;
return true;
}
@@ -704,7 +707,11 @@ public class PreferenceManager {
*/
void dispatchActivityDestroy() {
List<OnActivityDestroyListener> list = null;
if (mPreferenceScreen != null) {
mPreferenceScreen.onDetachedFromActivity();
mPreferenceScreen = null;
}
synchronized (this) {
if (mActivityDestroyListeners != null) {
list = new ArrayList<OnActivityDestroyListener>(mActivityDestroyListeners);