3

I have a RingtonePreference nested inside 2 PreferenceScreen declared as:

<PreferenceScreen
//other preferences
        <PreferenceScreen
        //other preferences
                <RingtonePreference
                    android:key="ringtone"
                    android:title="@string/ringtone_title"
                    android:summary="@string/sipringtone_ringtone_summary"
                    android:ringtoneType="ringtone"  />
        />
/>

The PreferenceFragment is nested inside a ActivityGroup as such. The path to the fragment is as followed: MainActivity(TabActivity)->Activity1(ActivityGroup)->Activity2(Activity)->PreferenceFragment

I did not write this code but am picking off where someone left off. The dialogs from clicking every preference was crashing the app because it did not like the context of Activity2. This was an ActivityGroup related issue that was resolved by forcing the context of each preference to the context of Activity1.

EditPreferences, ListPreferences, and CheckPreferences all worked as intended but RingtonePreference is giving me a lot of trouble. Although the dialog pops up to let me choose the selection of ringtones, it does not save the setting.

Selecting a ringtone from the list and pressing the OK button does not trigger onPreferenceChange() or onSharedPreferenceChanged(). I tried creating a custom RingtonePreference and overriding onSaveRingtone() but that did not get called at all. However, other methods like onPrepareRingtonePickerIntent() and onRestoreRingtone() did get called. I tried a bunch of other options that were mentioned on stack overflow but had no luck. I'm running out of ideas to get RingtonePreference to work and think starting my own ringtone picker using RingtoneManager is the best alternative. If anyone could give me some advise on how to get RingtonePreference to work then that'll be awesome.

Edit: I believe this is a context problem but I can't find out how to solve it yet.

kyrax
  • 1,192
  • 3
  • 13
  • 29

2 Answers2

0

I was unable to get the RingtonePreference to work but I did find some new details and an alternative. RingtonePreference looks like it starts a new activity for the dialog. If you have launchmode=singleInstance, that will mess up the RingtonePreference because you're starting that activity on a different Task stack. Using launchmode=singleTask or removing the launchmode could solve your problem, but not for me entirely (but I still needed launchmode to not be equal to singleInstance). My solution was to add the preference manually in java code. The steps were

1) Find your preference screen
2) Make a preference
3) Set your preference details, i.e. title & summary
4) (Optional) Arrange the order of your preference (google the setOrder function for preference)
5) Set the onPreferenceCLickerListener to the preference you created
6) Inside onPreferenceClick, Launch the ringtone picker dialog configured by RingtoneManager and start the activity from where ever you want.
7) Add the preference to your preference screen chosen in step 1.
8) On the activity/fragment where you started the ringtone picker, override on onActivityResult() and handle the chosen ringtone.

Good luck!

kyrax
  • 1,192
  • 3
  • 13
  • 29
0

kyrax's answer seems appropriate, although I didn't want to go through all the mess of creating a Preference programmatically and then inserting it that way.

To solve this problem, I started off with a complete XML and then I simply added the OnPreferenceChangeListener to the RingtonePreference. This can be done from your PreferenceFragment:

    Preference notificationSoundPref = findPreference("ringtone);
    notificationSoundPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // do what you need here
            return true;
        }
    });
dekaru
  • 345
  • 2
  • 10
  • I am no longer with the same code base, but onPreferenceChange() was not getting called for me. Thanks for your post and I hope your answer will help someone else. – kyrax Jan 14 '16 at 21:34