RingtonePreference not saving in PreferenceActivity - android

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);
}
}

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.

Related

SwitchPreference in pre Android 7 always switched off

I'm extending PreferenceFragmentCompat, in the onCreatePreferences method the preference are set using addPreferencesFromResource(R.xml.preferences).
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="disableAppWhenObscured"
android:title="#string/screen_settings_block_app_when_obscured" />
</PreferenceScreen>
As dependency I have the following entry:
implementation "androidx.preference:preference-ktx:1.0.0"
On devices post Android 6.X, everything works like expected. On devices with Android 6 and pre-6, the switches are always off and not changeable if clicked on it, even not changeable programmatically but the change listener gets fired.
You know what the problem is?
Edit: If I change the UI element to CheckBoxPreference, everything works as expected even in Android 6 and pre-6. Furthermore before I started using androidX the SwitchPreference worked as well.
Edit2: Found out that the switchView is null inside the SwitchPreference in the following method:
#Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
View switchView = holder.findViewById(AndroidResources.ANDROID_R_SWITCH_WIDGET);
syncSwitchView(switchView);
syncSummaryView(holder);
}
and this leads to not changeing the UI in the syncSwitchView method.
Using SwitchPreferenceCompat instead of SwitchPreference should resolve the problem.

Should I create an activity for each screen?

I will start with an example... If you go to Settings > Applications > Manage applications, a new screen will open with a list of the installed applications: if you click on any application of the list, it will open a new screen containing information about the application.
Well, some settings of my application must be managed through a list, and this list should behave like the above example. I have already created a PreferenceActivity with some categories, each of which has some items: when I click on one of these items, I would like it to open a new screen where the new data is placed on a list, just like the list of the applications of the above example. Moreover, when I click on any entry of this list, it will open a new screen in order to set some data.
How should I proceed? Should I create an activity for each screen?
Android was created this way, according to the documentation "An activity is a single, focused thing that the user can do.", so yes, you should have an activity for each screen.
This changed a little with Honeycomb with the introduction of Fragments, but if you're not developing for tablets you should keep the one page, one activity mindset on Android.
Generally you have each activity call by another, the caller is pushed onto a stack (unless the calling activity ask's to be removed) and goes dormant until it returns
Basically you create an Intent in Activity A to start Activity B, you can pass data by using startActivityForResult with extras in the intents Example: How to pass data between activities
When you press the back button then that previous activity becomes active again and the result handler you set up can get any return data.
You might also look at fragments in the support API if you want to provide tablet support that looks and behaves better.
That is propably the best way to do it, at least if you're not working on a wizard style activity.
Use a ListActivity to show your list, and pass data to and from this activity using intents.
I was able to implement this at work, I don't remember right now in the head how I implemented it, was long time ago. If nobody has a good answer for you I will post it tomorrow, however: I remember putting a Preference, which will act as a button, then I added a preferenceClickListener in order to open a new PreferenceScreen on click.
But like I said, I'll post it for you tomorrow if you don't get a satisified answer.
Good luck!
UPDATE:
?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Personal"
android:key="personal_category">
<Preference
android:key="birth"
android:title="Birth"
android:summary="Choose your birthday"/>
<PreferenceScreen
android:key="height_imp"
android:title="Height"
android:summary="Enter your height">
<EditTextPreference
android:key="foot"
android:title="Foot"
android:summary="foot"
android:numeric="integer"
android:dialogTitle="Foot"/>
<EditTextPreference
android:key="inch"
android:title="Inch"
android:summary="inch"
android:numeric="integer"
android:dialogTitle="Inch"/>
</PreferenceScreen>
<EditTextPreference
android:key="weight"
android:title="Weight"
android:summary="Enter your weight"
android:numeric="integer"
android:dialogTitle="Weight"/>
</PreferenceCategory>
</PreferenceScreen>
That's it! When you click on it, it will take you to the second PreferenceScreen and so on, then finally when you need to customize your layout you'll need to open an Activity.
You could then use a Preference and add onPreferenceClick:
#Override
public boolean onPreferenceClick(Preference preference) {
if(preference == birth){
startActivity(new Intent(getBaseContext(), Birth.class));
}
if(preference == height_imp){
PreferenceScreen a = (PreferenceScreen) preference;
a.getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
return false;
}
return true;
}
If you need to change the background or something else with the preferenceScreen, then add a preferenceClickListener as well: height_imp = (PreferenceScreen)getPreferenceScreen().findPreference("height_imp");
height_imp.setOnPreferenceClickListener(this);
See... if once the user wants to return from certain point to previous position... if you had created a seperate activity for each of them... the present activity will be popped off the stack... letting the previous activity to be displayed...If you are changing the content of the list for every new screen...instead of creating new activity... then it will be difficult for the user to come back... you should again and again change the content of adapter..
So I think.. creating seperate activity for each screen is better..( and you can use same [any custom layout if you have]layout file for all activities..)

Hide item in Android preferences menu

my Application has a Preferences menu. there are 2 checkbox: "Enable Music", and "Show Hints". What I want is, There is Music List will show if check "Enable Music", else Music list will not enable. My code is:
boolean OPT_MUSIC_DEF = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(OPT_MUSIC, OPT_MUSIC_DEF)){
Preference list = findPreference("music_list");
list.setEnabled(false);
}
}
There is no error, but it work incorrect. When I unCheck "Enable Music" Music List did not hide right away, I must back and go to menu again to see Music List is hide.
Any way to make it hidden directly after uncheck "Enable Music" item?
thank for help me!
There is built in functionality to do exactly what you're trying to do. In your preferences.xml file set the dependency for the music_list preference to depend on the music_enable preference.
For example:
<android.preference.CheckBoxPreference
android:key="music_enable"
android:defaultValue="false"
android:title="Enable Music"
/>
<android.preference.ListPreference
android:key="music_list"
...
android:dependency="music_enable"
/>
Whenever the user checks the "music_enable" check box the "music_list" will be enabled; when they uncheck the "music_enable" check box the "music_list" will be disabled.
Make sure to call onContentChanged() right after disabling the preference.
If you want to force refresh the appearance of your preference, just do:
Preference myPref = findPreference("mypref");
myPref.setDefaultValue((int)(Math.random()*1000); // you can also backup the value before and reset it after if you need to keep a default value
Not the perfect solution, but it will surely force Android to refresh your preferences.
BTW, I tried Paul-Jan solution but unluckily it didn't work, it seems that onContentChanged() will just do nothing if no value is changed.

How to Initialize Preferences Screen on First Invocation?

I believe I am correctly initializing preferences from XML. My Preferences Screen also works properly and reflects the correct user selected settings.
However, upon first invocation of that Preferences Screen, none of the settings are checked (checkbox) or selected (list). This, of course, confuses the user as it doesn't reflect the current (default/initial) value.
Since all I do to invoke the Preferences Screen is
startActivity(new Intent(this, EditPreferences.class));
And my EditPreferences class only contains:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.usersettings);
}
I am not sure where or how to tell it to pre-initialize the visual display with the default setting.
I have this hunch that all I am missing is a single line somewhere, but I don't know where: XML file? override a method in EditPreferences? Other?
Can't you define the default value in the XML itself?
<CheckBoxPreference ...
android:defaultValue="true"
... />
You can specify a default value on a preference (in your xml layout for example):
<EditTextPreference android:defaultValue="whatever" />

Why doesn't my preferences page show the current values?

I have a preferences page which is defined by XML - including some default values. I use a PreferenceActivity to display and handle this page. Whenever I use this page to set the preferences the preference file on the file system is updated properly - I can see this via adb.
However, whenever I go back to the settings page after have changed some of the settings, it's the defaults that are shown. Worse than that, if I press back without changing any settings, it then sets the, all back to the default.
Any ideas on how I can get the prefs to actually show the current settings?
My PreferenceActivityis created thus:
#Override
protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
setDefaults(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:id="#+id/numberOfYearsList"
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="#array/numberOfYears"
android:entryValues="#array/numberOfYears"
android:dialogTitle="How Many Years?"
android:defaultValue="1"
/>
<CheckBoxPreference android:key="ignoreDates"
android:id="#+id/ignoreDatesCheckbox"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
android:defaultValue="false"
/>
</PreferenceScreen>
Nevermind - I did a mistake. As you can see from above, when creating the Preference Activity I'm making a call to a method that sets all the prefs to defaults... Duh!

Categories

Resources