I have the following preference:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <RingtonePreference android:showDefault="true" android:showSilent="true" android:title="@string/feed_alert_ringtone" android:ringtoneType="ringtone|notification|alarm|all" android:key="alertringtone" android:persistent="false"> </RingtonePreference> </PreferenceScreen>
When the user changes this preference, I manually save the Uri in the database:
public boolean onPreferenceChange(Preference pref, Object change) { String ringtone = change.toString();
My problem is that when the user closes and then returns to PreferenceScreen , the default RingtonePreference always Silence . Of course, I need to set the value manually.
Tried this in my PreferenceActivity :
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings);
Unfortunately, the preference remains in "Silence" (which means "empty"). I thought that when the Preference not persistent , the default value will be used.
I cannot do this persistent , because preference is used for several elements, and the data that I get from the database changes according to some additional data passed to the intent.
However, I do not want to set the default value, but the current value that I get from the database. Thought it could be a workaround. So any other way will be good too.
Double-checked saving and retrieving from the database works, so no problem.
How can I set the RingtonePreference value from code?
Edit: here is a small explanation of what I want to do, maybe there is another way.
I have several menu entries (the number varies from user to user) for which the user can select a ringtone for the call. Selecting a ringtone works with the xml above, the identifier of the menu item is passed from Intent to PreferenceActivity . The ringtone URI is then stored in the database.
When one of the recordings changes, the sound signal is played in accordance with the ringtone selected by the user. Therefore, he knows which one has changed. There are no problems so far.
The problem is when the user wants to change the ringtone for the recording for which he has already defined. If the user has selected the Foo ringtone before, when clicking on the RingtonePreference , the Foo sound should be preselected. This is not a serious mistake (rather, it seems like a failure), but still very annoying.
As far as I can tell, there is no way to pre-select the default RingtonePreference from the code? Or am I doing this "just wrong"?
Edit 2: Okey, I think there is no way to do this. Very strange, the core of the android. Allows us to use Preference and get values ββfrom it, but not return values. It must have been a real rush. I am glad that they allowed us to check the CheckBoxPreference code ...