maintain state when rotating settings activity

This commit is contained in:
iNPUTmice 2015-04-14 15:53:50 +02:00
parent b6cc8c90a0
commit f99e234b88
2 changed files with 26 additions and 18 deletions

View File

@ -7,6 +7,8 @@ import java.util.Locale;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build; import android.os.Build;
@ -21,9 +23,12 @@ public class SettingsActivity extends XmppActivity implements
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mSettingsFragment = new SettingsFragment(); FragmentManager fm = getFragmentManager();
getFragmentManager().beginTransaction() mSettingsFragment = (SettingsFragment) fm.findFragmentById(android.R.id.content);
.replace(android.R.id.content, mSettingsFragment).commit(); if (mSettingsFragment == null || !mSettingsFragment.getClass().equals(SettingsFragment.class)) {
mSettingsFragment = new SettingsFragment();
fm.beginTransaction().replace(android.R.id.content, mSettingsFragment).commit();
}
} }
@Override @Override
@ -34,18 +39,15 @@ public class SettingsActivity extends XmppActivity implements
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
PreferenceManager.getDefaultSharedPreferences(this) PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
.registerOnSharedPreferenceChangeListener(this); ListPreference resources = (ListPreference) mSettingsFragment.findPreference("resource");
ListPreference resources = (ListPreference) mSettingsFragment
.findPreference("resource");
if (resources != null) { if (resources != null) {
ArrayList<CharSequence> entries = new ArrayList<CharSequence>( ArrayList<CharSequence> entries = new ArrayList<>(Arrays.asList(resources.getEntries()));
Arrays.asList(resources.getEntries())); if (!entries.contains(Build.MODEL)) {
entries.add(0, Build.MODEL); entries.add(0, Build.MODEL);
resources.setEntries(entries.toArray(new CharSequence[entries resources.setEntries(entries.toArray(new CharSequence[entries.size()]));
.size()])); resources.setEntryValues(entries.toArray(new CharSequence[entries.size()]));
resources.setEntryValues(entries.toArray(new CharSequence[entries }
.size()]));
} }
} }

View File

@ -36,7 +36,9 @@
android:title="@string/pref_chat_states" /> android:title="@string/pref_chat_states" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_notification_settings" > <PreferenceCategory
android:title="@string/pref_notification_settings"
android:key="notifications">
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="true" android:defaultValue="true"
android:key="show_notification" android:key="show_notification"
@ -45,7 +47,8 @@
<PreferenceScreen <PreferenceScreen
android:dependency="show_notification" android:dependency="show_notification"
android:summary="@string/pref_quiet_hours_summary" android:summary="@string/pref_quiet_hours_summary"
android:title="@string/title_pref_quiet_hours"> android:title="@string/title_pref_quiet_hours"
android:key="quiet_hours">
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="enable_quiet_hours" android:key="enable_quiet_hours"
@ -108,10 +111,13 @@
android:summary="@string/pref_show_dynamic_tags_summary" android:summary="@string/pref_show_dynamic_tags_summary"
android:title="@string/pref_show_dynamic_tags" /> android:title="@string/pref_show_dynamic_tags" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_advanced_options" > <PreferenceCategory
android:title="@string/pref_advanced_options"
android:key="advanced">
<PreferenceScreen <PreferenceScreen
android:summary="@string/pref_expert_options_summary" android:summary="@string/pref_expert_options_summary"
android:title="@string/pref_expert_options" > android:title="@string/pref_expert_options"
android:key="expert">
<PreferenceCategory android:title="@string/pref_encryption_settings" > <PreferenceCategory android:title="@string/pref_encryption_settings" >
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"