RingtonePreference doesn't save its value

痞子三分冷 提交于 2019-12-10 17:46:55

问题


I already worked with some preferences, such as EditTextPreference, etc, and all they work, but now I need to use RingtonePreference, and it does not work properly. Here's very simple code:

  <RingtonePreference
     android:key="my_ringtone_key"
     android:title="Select ringtone"
     />

When I click on this preference, id does open dialog to select ringtone, but it does not save it. I mean, I select ringtone, press "ok", then click this preference again, and still no ringtone selected.

If I try to get value from code, like that:

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String tmp = prefs.getString("my_ringtone_key", "none");

Then tmp have default value "none", which means that value completely isn't stored in SharedPreferences.

BUT: if I change RingtonePreference to EditTextPreference, i.e. the following:

  <EditTextPreference
     android:key="my_ringtone_key"
     android:title="Select ringtone"
     />

Then all works: I can edit the value, value is stored, and of course I can get it from code.

I use API level 7. I tested this on emulator and two devices, SE Xperia Neo and some Acer.

So, how get RingtonePreference to work? And please add comment if RingtonePreference does work for you or not.


回答1:


Try removing noHistory="true", android:launchMode="singleInstance", and/or android:excludeFromRecents="true" from the affected <activity> entry in your manifest.

The launchMode entry solved the problem for me; I found these solutions recently posted as answers here.




回答2:


I had the same issue. My mistake was overriding the PreferenceFragment's onActivityResult method without calling super.onActivityResult, which is where the selected ringtone gets saved.

see https://stackoverflow.com/a/10020414/213817



来源:https://stackoverflow.com/questions/12052096/ringtonepreference-doesnt-save-its-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!