Reload Preferences without restarting the app

0

I have an App where I synchronize data with an online server and the user can select what he wants to synchronize and also has the encryption keys that have to be set up so sync can occur, my question is the following, whenever I mark or unselect a sync option or mute the encryption key, I need to close the app and destroy it, and then reopen it so that the preferences run successfully, there is another way to do this dynamically? I already tried apply, commit and nothing. Follow the OnChangePreferenceListener code:

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();

        if (preference.getKey().compareTo("url_servidor")==0) {
            String regex = "(https?)://[a-zA-Z0-9:.-]*(/[a-zA-Z0-9+&@#%=~_|]{2,})*[^/]$";
            if (!stringValue.matches(regex)) {
                Toast.makeText(ApplicationContextProvider.getContext(), "URL inválida !!!", Toast.LENGTH_SHORT).show();
                return false;
            }
        }

        if (preference.getKey().compareTo("usuario")==0) {
            if (stringValue.length() == 0) {
                Toast.makeText(ApplicationContextProvider.getContext(), "Usuario invalido !!!", Toast.LENGTH_SHORT).show();
                return false;
            }
        }

        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference
                    .setSummary(index >= 0 ? listPreference.getEntries()[index]
                            : null);
        } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
            PreferenceManager.getDefaultSharedPreferences(preference.getContext()).edit().putString(preference.getKey(), stringValue);
        }
        return true;
    }
};
    
asked by anonymous 01.10.2018 / 21:45

0 answers