-1

I am using PreferenceScreen to set some user preferences in my Android app. It works perfectly for several ListPreference and CheckboxPreference items, however I cannot get RingtonePreference to work properly. The appropriate ringtone dialog displays, and can be selected, but the selection is never saved.

My app only plays the default sound no matter what I select. Whenever I re-open the ringtone dialog (either immediately after making a selection, or after exiting the app and coming back in) it always just has the default item selected. I have a field to display the preference value, and it always shows the default sound is selected, even after changing it on the preferences screen. I also confirmed that the appropriate xml file on my device (in /data/data/myapp/shared_prefs) is not changing when I monitor it with DDMS. If I change other items (such as a CheckboxPreference), I see the shared_prefs file change realtime. I have stripped my PreferenceScreen to the bare minimum and it still behaves the same. I tried changing key names, defaults, and clearing app data on my phone.. nothing seems to work.

I did find 2 similar questions already posted on SO (here and here), but they are several months old, with no answers and/or the person asking the question gave up and tried another method. I'd like to figure out why it does not appear to work as designed.. or at least, find a suitable method to get the same thing done.

The relevant portions of my code are below.. thanks in advance!

/res/xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  <RingtonePreference
    android:key="alertSound"
    android:ringtoneType="notification"
    android:summary="Select audio notification sound"
    android:title="Alert Sound" >
  </RingtonePreference>
</PreferenceScreen>

/src/myapp/EditPrefsActivity.java:

public class EditPrefsActivity extends PreferenceActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    settings = PreferenceManager.getDefaultSharedPreferences(this);
    addPreferencesFromResource(R.xml.preferences);
  }
}
Community
  • 1
  • 1
mike47
  • 2,217
  • 1
  • 27
  • 50
  • I have continued to tweak this without any luck. Just to clarify, my Preferences Screen has 6 different items; the other 5 work perfectly. I set up an OnSharedPreferenceChangeListener which properly triggers for the 5 working preference items, however it never runs for this RingtonePreference. It's essentially behaving like I'm clicking Cancel instead of OK on the ringtone selection dialog. The dialog appears correctly, all of the notification ringtones are in the list, and play if you click them.. but the selection is never saved. – mike47 Feb 18 '13 at 18:54
  • My `Preference` sample apps all are able to retain their `RingtonePreference`. Try one, like https://github.com/commonsguy/cw-omnibus/tree/master/Prefs/FragmentsBC and see if it works for you. If it works, then you can start trying to narrow down the differences between your implementation and mine. – CommonsWare Feb 27 '13 at 12:49
  • Thanks for the help, @CommonsWare, it actually turned out to be an obscure Android bug tied to launchMode. I posted the solution below. – mike47 Jun 12 '13 at 00:20

1 Answers1

1

I just found the solution! The answer was recently posted in a similar question: https://stackoverflow.com/a/15887357/1992342

Removing android:launchMode="singleInstance" from the EditPrefsActivity <activity> entry in the manifest solves the problem. Apparently it is a [little known] Android bug.

Community
  • 1
  • 1
mike47
  • 2,217
  • 1
  • 27
  • 50