Android Delete Preference Detailed _android

Source: Internet
Author: User
Tags delete key

The Android Setup interface is simple to implement, and sometimes it just needs a simple XML file. The declaration is simple, But how to remove a preference from a preferencescreen or preferencecategory is easy. Why do some people write can not delete success? This article will be from the Android source implementation to analyze.

Declaration file

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android"
android:key= "Root" >

<preferencecategory
android:key= "Theme"
Android:title= "Theme"
android:summary= "Theme Settings"
>
<checkboxpreference
android:key= "Holo_theme"
android:title= "Holo Theme"
android:summary= "Use Holo Theme"
/>

</PreferenceCategory>

<checkboxpreference
android:key= "Rmcache"
Android:title= "Auto Clear Cache"
android:summary= "Enable Auto Clear Cache"
/>
</PreferenceScreen>

Hierarchical relationship

Delete Preference

Deletes the key as Rmcache preference, which is the child node of Preferencescreen root.

Copy Code code as follows:

Preferencescreen screen = Getpreferencescreen ();
Checkboxpreference autoclearcheckboxpref = (checkboxpreference) screen.findpreference ("Rmcache");
Screen.removepreference (AUTOCLEARCHECKBOXPREF);

Delete key is Holo_theme preference, which is preferencescreen root of the grandson node, not directly related.

Copy Code code as follows:

Preferencecategory themeprefcategory = (preferencecategory) screen.findpreference ("theme");
Checkboxpreference holocheckboxpref = (checkboxpreference) themeprefcategory.findpreference ("Holo_theme");
Themeprefcategory.removepreference (HOLOCHECKBOXPREF);

Why delete failed

Many people have failed to delete the problem, mainly because of the use of a non-father node to delete, such as

Copy Code code as follows:

Preferencescreen screen = Getpreferencescreen ();
Checkboxpreference holocheckboxpref = (checkboxpreference) screen.findpreference ("Holo_theme");
Screen.removepreference (HOLOCHECKBOXPREF);

Preferencegroup Delete implementation, in fact, Preferencescreen and Preferencecategory are preferencegroup subclasses.

Copy Code code as follows:

/**
* Removes a {@link preference} from this group.
*
* @param preference the preference to remove.
* @return Whether The preference was found and removed.
*/
public boolean removepreference (preference preference) {
Final Boolean returnvalue = removepreferenceint (preference);
Notifyhierarchychanged ();
Return returnvalue;
}

Private Boolean removepreferenceint (preference preference) {
Synchronized (this) {
Preference.onprepareforremoval ();
return Mpreferencelist.remove (preference);
}
}

In Mpreferencelist, the direct preference of the current preferencegroup is stored.

Findpreference implementation

The Findpreference lookup is not limited to the direct child preference, and iterates through all of its child preference.

So the code also has root Preferencegroup and direct parent Preferencegroup references, which are usually highly efficient.

Copy Code code as follows:

/**
* Finds a {@link preference} based on its key. If two {@link preference}
* Share the same key (not recommended), the the ' the ' to appear would be
* Returned (to retrieve the "other preference" with the same key.
* method on the the-preference. If This preference has the key, it would
* Not to be returned.
* <p>
* This'll recursively search for the preference to children that are
* Also {@link preferencegroup preferencegroups}.
*
* @param key The key of the preference to retrieve.
* @return The {@link preference} with the key, or null.
*/
Public preference Findpreference (charsequence key) {
if (Textutils.equals (Getkey (), key)) {
return this;
}
Final int preferencecount = Getpreferencecount ();
for (int i = 0; i < Preferencecount; i++) {
Final Preference preference = getpreference (i);
Final String Curkey = Preference.getkey ();

if (Curkey!= null && curkey.equals (key)) {
return preference;
}

if (preference instanceof Preferencegroup) {
Final Preference Returnedpreference = ((preferencegroup) preference)
. findpreference (key);
if (returnedpreference!= null) {
return returnedpreference;
}
}
}

return null;
}

Comparison of Findpreference and Removepreference implementations

Why Findpreference Traverse all the child nodes, while Removepreference does not, only deletes the direct child preference

There are several reasons for this:

1.findPreference supports traversal lookups, reducing the number of intermediate Preferencegroup code that is declared. and findpreference belong to common interface methods.
Fewer 2.removePreference calls.
3. When there is a key of the same preference, if the removepreference does not limit the direct child preference, then can not exactly delete which one.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.