fix crash when toggling setting after rotation

This commit is contained in:
Daniel Gultsch 2024-04-08 10:05:07 +02:00
parent 67e01af5ef
commit 03ee84bf35
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 16 additions and 6 deletions

View file

@ -26,11 +26,15 @@ public class ConnectionSettingsFragment extends XmppPreferenceFragment {
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.preferences_connection, rootKey);
final var connectionOptions = findPreference(AppSettings.SHOW_CONNECTION_OPTIONS);
final var channelDiscovery = findPreference(AppSettings.CHANNEL_DISCOVERY_METHOD);
final var groupsAndConferences = findPreference(GROUPS_AND_CONFERENCES);
if (channelDiscovery == null || groupsAndConferences == null) {
if (connectionOptions == null || channelDiscovery == null || groupsAndConferences == null) {
throw new IllegalStateException();
}
if (QuickConversationsService.isQuicksy()) {
connectionOptions.setVisible(false);
}
if (hideChannelDiscovery()) {
groupsAndConferences.setVisible(false);
channelDiscovery.setVisible(false);

View file

@ -1,10 +1,12 @@
package eu.siacs.conversations.ui.fragment.settings;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceFragmentCompat;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.XmppActivity;
@ -17,10 +19,14 @@ public abstract class XmppPreferenceFragment extends PreferenceFragmentCompat {
if (key == null) {
return;
}
onSharedPreferenceChanged(key);
if (isAdded()) {
onSharedPreferenceChanged(key);
}
};
protected void onSharedPreferenceChanged(@NonNull String key) {}
protected void onSharedPreferenceChanged(@NonNull String key) {
Log.d(Config.LOGTAG,"onSharedPreferenceChanged("+key+")");
}
public void onBackendConnected() {}
@ -43,7 +49,7 @@ public abstract class XmppPreferenceFragment extends PreferenceFragmentCompat {
super.onPause();
final var sharedPreferences = getPreferenceManager().getSharedPreferences();
if (sharedPreferences != null) {
sharedPreferences.registerOnSharedPreferenceChangeListener(
sharedPreferences.unregisterOnSharedPreferenceChangeListener(
this.sharedPreferenceChangeListener);
}
}

View file

@ -2,13 +2,13 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/pref_category_server_connection">
<CheckBoxPreference
<SwitchPreferenceCompat
android:defaultValue="@bool/show_connection_options"
android:icon="@drawable/ic_settings_24dp"
android:key="show_connection_options"
android:summary="@string/pref_show_connection_options_summary"
android:title="@string/pref_show_connection_options" />
<CheckBoxPreference
<SwitchPreferenceCompat
android:defaultValue="@bool/use_tor"
android:icon="@drawable/ic_network_node_24dp"
android:key="use_tor"