Users can add projects in my app and I have a PreferenceFragment for settings of the projects.
This is my Fragment:
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.project_settings);
}
}
And my Activity:
public class Settings extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
The availabal settings for each project are identical but the data should be saved per project (e.g. the project name). So how can I save the preferences to my sqlite database?
Putting this here as none of the other questions/answers on this topic were satisfactory, and I just finished precisely this functionality (connect Preferences in a PreferenceFragment to the database).
Basically, you should set android:persistent="false" for each Preference in the XML. You should then load values into the Preferences manually on onCreate(), and use setOnPreferenceChangeListener() to do database value-updating and Preference summary field updating. You may also have to extend some existing Preferences to allow them to be set with values manually, as some of them (e.g. the RingtonePreference) do not have methods to set their values -- they just get them internally from SharedPreferences.
The answer is a Preference.OnPreferenceChangeListener and some magic inside the onPreferenceChange method.
Related
I got some nested PreferenceScreen in my PreferenceActivity, and under them a series of CheckBoxPreference.
Everything is working well but, whenever the device is rotated, the PreferenceActivity returns to the main PreferenceScreen, disregarding what nested preference screen the user was in.
This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen:
When my PreferenceActivity rotates, it does not remember which
PreferenceScreen was open
How to prevent quitting from inner preference screen when there's a
configuration change
Nested Preference Screen closes on Screenorientation change in
Android
I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way:
public class Settings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
The problem is I'm using a PreferenceFragment, like in this SO answer (and also here).
The code:
public class Settings extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
}
So, how to keep inner PreferenceScreen open after screen orientation changes, if possible with the current code?
I see that for Preferences Google now recommends AppCompatActivity and PreferenceFragmentCompat, but I would prefer to not use any libraries, not even Google's, specially for only such a small detail.
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null) before adding the PreferenceFragment, like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key value set in, of course.
I have an app which uses preferenceScreen (checkboxes) for the user to turn on and off certain options. The preferences fragment which inflates the xml file looks like this:
public class BrandsFragment : PreferenceFragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
AddPreferencesFromResource (Resource.Menu.brandPrefs);
var prefs = PreferenceManager.GetDefaultSharedPreferences(this.Activity);
}
}
This works fine and i can successfully change the preferences.
However, what i need to do is get the preferences from the OnCreate method in my Main activity (not fragment).
This is what i have tried:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var prefs = PreferenceManager.GetDefaultSharedPreferences(this);
wantGeneral = prefs.GetBoolean ("checkbox_preference_general", true);
Console.WriteLine ("Pref is " + wantGeneral);
SetContentView(Resource.Layout.NavigationDrawer);
.......
}
But the 'wantGeneral' preference always comes back true, regardless of the checkbox being checked. It's clear that from my Activity, it's not successfully grabbing the preferences.
What do i need to do in order to get the preferences from the Activity?
It appears that it was me not thinking it through properly. I needed to move the code into OnResume() or somewhere else that gets called after i make change to the preferences in my settings page. That way it pulls in the latest settings.
I am doing an app with preferences but I have used a method that is deprecated and it says :
"This function is not relevant for a modern fragment-based PreferenceActivity". My code is this:
public class Settings extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
How can I update this to not deprecated function. Thank you very much.
The new way is to do the Preferences in an Fragment instead of an Activity. This is espacially true for large screens and tablets. Fragments can be shown separate or next to each other over an Activity according to screen size. Use them like this:
public static class YourPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
and instead of calling the PreferenceActivity you make a call to the Fragment in your Activity:
YourPreferenceFragment prefFragment = new YourPreferenceFragment();
prefFragment.show(getFragmentManager(), "someFragmentId");
Try to use PreferenceFragment instead.
Check this: http://developer.android.com/guide/topics/ui/settings.html
PreferenceActivity is depricated, you can use PreferenceFragment instead.
here are some tutorials
Link 1
Link 2
Here is the documenattion for PreferenceFragment
I am new to Android and I am trying to handle the settings according to the instructions in
http://developer.android.com/guide/topics/ui/settings.html
I used the fragment based solution as the one directly in the activity was deprecated:
public class SettingFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingFragment())
.commit();
}
}
The settings are correctly displayed in the app, yet when I try to retrieve their values with:
sharedPref= PreferenceManager.getDefaultSharedPreferences(Dashboard.dashboard);
startDayTime=sharedPref.getString(SettingsActivity.day_switch_start, "");
startNightTime=sharedPref.getString(SettingsActivity.night_switch_start, "");
it complaints that values day_switch_start and night_switch_start might not be resolved. In fact, when I explode the values recognized by SettingActivity, I get a long list of capitalized strings part of the standard settings, but not my own strings.
Those were in fact just entered in the xml file and linked from the SettingFragment, so I doubt context Dashboard.dashboard I passed has any reference to it. Yet there are no instructions on how to pass the settings references to a specific context and moreover they say that the settings should be available from anywhere. I am stuck, any solution?
Thanks,
Please check the reference you linked again, especially the Reading Preferences section. When you set the preference key to, e.g., day_switch_start then you must use the same String when reading the preference, e.g.,
startDayTime=sharedPref.getString("day_switch_start", "");
You can also store the preference key to a constant like in the linked example:
public class SettingsActivity extends PreferenceActivity {
public static final String KEY_PREF_DAY_SWITCH_START = "day_switch_start";
// ...
and then access it when reading the preference:
startDayTime=sharedPref.getString(SettingsActivity.KEY_PREF_DAY_SWITCH_START, "");
Edit: I'm not sure what Dashboard.dashboard should represent but this should work:
sharedPref= PreferenceManager.getDefaultSharedPreferences(this);
in the onCreate method of my PreferenceActivity i set some Properties like this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("pref_mykey", true);
editor.commit();
It seems to work but the View is not updating so when i open the properties the onCreate runs and changes the values. I will then see the "old" values on the Screen until i close and reopen the prefs screen, then i do see the new properties.
I already tried this after the commit:
((BaseAdapter) getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
But without any effect. I still see the "old" Prefs until i close and reopen the PreferenceActivity .
What am i doing wrong? How can i refresh the PreferenceActivity after setting values in the onCreate method?
Thanks a lot for your help.
This was buggin me all day but finally found a solution.
((ListPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setValue(code);
Your example is with a boolean so it'll have to change based on what kind of preference it is.
IE:
((CheckBoxPreference)getPreferenceScreen().findPreference("key_of_preference_in_xml_file")).setChecked(true);
If you use a PreferenceFragment than you need to inject the PreferenceScreen in the activity.
public class SettingsActivity extends Activity implements ScanFragment.OnFragmentInteractionListener {
private PreferenceScreen prefScreen;
public void setPreferenceScreen(PreferenceScreen prefScreen){
this.prefScreen = prefScreen;
}
#Override
public void onFragmentInteraction(String scanContent) {
// commiting to editor retrieved from preference manager would not refresh view
EditTextPreference pref = (EditTextPreference)this.prefScreen.findPreference(getString(R.string.preference_url_key));
pref.setText(scanContent);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
((SettingsActivity)this.getActivity()).setPreferenceScreen(this.getPreferenceScreen());
}
}
}