Mickey Tin
Mickey Tin

Reputation: 3533

Restore preference after it removing

In PreferenceActivity I get the Preference myPref by calling myPref = findPreference(..) after that I check for some condition and remove it from "PreferenceList" like this : getPreferenceScreen().removePreference(myPref); in second call of my method I need to add that Preference again but findPreference(..) returns null.How do I restore that myPref for adding it later to the "PreferenceList" by calling getPreferenceScreen().addPreference(myPref); ?

Preference myPref= findPreference( getString( R.string.path_preference ));//return null after removing

        if (condition) {
            if (!pbePathPreferenceRemoved) {
                getPreferenceScreen().removePreference(myPref);
                pbePathPreferenceRemoved = true;
            }
        } else if (pbePathPreferenceRemoved) {
            getPreferenceScreen().addPreference(myPref);
            pbePathPreferenceRemoved = false;
        }

Upvotes: 2

Views: 298

Answers (1)

Mickey Tin
Mickey Tin

Reputation: 3533

My solution is to make the Preference myPref member of my PreferenceActivity class and initialize it only once in onCreate(..) method, after that I can remove it and add again.

Upvotes: 1

Related Questions