I wanted to open ListPreference without PreferenceActivity. But as I use ActionBar Sherlock and don't have time to write custom ListView to work as ListPreferences I decided to use temporary solution from this thread: How to open or simulate a click on an android Preference, created with XML, programmatically?
I have PreferenceActivity:
public class PrefsActivity extends SherlockPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
PreferenceScreen preferenceScreen = getPreferenceScreen();
int pos = findPreference("timeZone").getOrder();
preferenceScreen.onItemClick(null, null, pos, 0);
}
}
And prefs.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="mainPreferences" >
<ListPreference
android:defaultValue="Default"
android:entries="@array/prefs_time_zones_names"
android:entryValues="@array/prefs_time_zones_values"
android:key="timeZone"
android:summary="@string/prefs_time_zones_summary"
android:title="@string/prefs_time_zones_title" />
(...)
(...)
</PreferenceScreen>
When I start PrefsActivity, my ListPreference (with key "timeZone") appears and works correctly. The problem occurs when I rotate the phone while the list is open. After the rotation, the second list is created. So when I choose an item from the list and the list disappear, the second one is still present (with original item selected). How to avoid creating the second list while the rotation?