Sunday, 15 September 2013

Custom ListPreference data not stored

Custom ListPreference data not stored

I have a custom LocationsListPreference class written for multiple section
by extending ListPreference. The data you select via multiple selection in
popup saves if im in the same activity., But if i go back to the main
activity and come back the values i have saved form the multiple selection
are not stored. Any idea why?
LocationsListPreference XML:
<com.gm.settings.LocationsListPreference
android:defaultValue="#ALL#"
android:key="list_locations"
android:title="@string/LocationsListPreference_title"
android:dialogTitle="@string/LocationsListPreference_title"
android:summary="@string/LocationsListPreference_summary"
/>
Here a bit of source code of my LocationsListPreference class for the
above ListPreference:
public class LocationsListPreference extends ListPreference {
//Need to make sure the SEPARATOR is unique and weird enough that it
doesn't match one of the entries.
//Not using any fancy symbols because this is interpreted as a regex
for splitting strings.
private static final String SEPARATOR = CommonConstants.SEPARATOR;
private static String val;
private boolean[] mClickedDialogEntryIndices;
public LocationsListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mClickedDialogEntryIndices = new
boolean[CommonConstants.LOCATIONS_KEYS.length];
}
@Override
public void setEntries(CharSequence[] entries) {
super.setEntries(entries);
mClickedDialogEntryIndices = new boolean[entries.length];
}
public LocationsListPreference(Context context) {
this(context, null);
}
Here is the "Settings" activity which i use to load PreferenceFragment:
public static void launch(Context context)
{
contxt = context;
Intent prompt = new Intent(contxt, Settings.class);
prompt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_NO_USER_ACTION);
contxt.startActivity(prompt);
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_settings);
}
}
Now if i select values from the ListPreference it successfully stores even
if i re-click from within the "Settings" activity. But if i go to another
activity and come back to "Settings" activity and load the ListPreference,
those values are not stored but shows all unchecked. Any ideas as to why
this happens?

No comments:

Post a Comment