PreferenceManager.setDefaultValues in a PreferenceFragment - android

I have a PreferenceFragment and I want to set the default values according to an XML file. Here is my onCreate method:
public class SettingsFragment extends PreferenceFragment {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
//This is the faulty line
PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
}
Unfortunately, it does not compile because "this" is not a proper context.
How should this be done?

This is because the first parameter of setDefaultValues is a Context. A PreferenceFragment is not a Context so use this.
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, true);

use getActivity() as you are in a fragment, not a Context.

You will have to use getActivity() to get a Context inside a Fragment. Note that you can only use this Method safely after onActivityCreated(Bundle) was called by the system.
So you might want to use something like the following:
public class SettingsFragment extends PreferenceFragment {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
//This was the faulty line
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, true);
}
}

Related

How to create a PreferenceScreen in a PreferenceActivity?

Consider the following activity:
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen screen =
getPreferenceManager().createPreferenceScreen(this);
}
}
Android Studio shows a warning for the call to getPreferenceManager():
'getPreferenceManager()' is deprecated
This inspection reports where deprecated code is used in the specified inspection scope.
However, it does not describe what corrective action I should take to avoid the warning. I can't find any alternative for obtaining a reference to the PreferenceManager and I see no other way to create a PreferenceScreen.
My goal is to programatically populate the PreferenceActivity with preferences and their default values since these are generated at runtime and cannot be included in xml/preferences.xml.
Although getPreferenceManager() is deprecated in SettingsActivity, it is not deprecated in PreferenceFragment. Therefore, the correct way to create a PreferenceScreen is as follows:
public class SettingsActivity extends PreferenceActivity {
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen screen =
getPreferenceManager().createPreferenceScreen(getActivity());
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}

Right way to initialize preference XML for PreferenceFragmentCompat

In the Android documentation available here, their example sets the XML for PreferenceFragmentCompat this way:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
But in this new support class, we must override the method onCreatePreferences, where the same XML file can also be inflated:
#Override
public void onCreatePreferences(Bundle bundle, String s) {
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
What is the point of this new method that we have to override since the old way is still working?

Preferences activity deprecated

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

Add preferences deprecated

I just noticed that addPreferencesFromResource(); is deprecated in Android now. I tried looking for what I should use now and the developers site still has it this way. Does anyone know what I should use instead?
As I pointed out in my comment, just use a Preference fragment
PreferenceActivity is still fine, but you have to use a PreferenceFragment subclass to do it now. It looks something like this:
public class SetupActivity extends PreferenceActivity {
#Override
protected void onCreate(final 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);
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
}
}
You then store the preferences in preferences.xml like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="rest_server"
android:defaultValue="#string/default_rest_server"
android:summary="#string/rest_url_desc"
android:title="#string/rest_url_title" />
<EditTextPreference
android:key="base_url"
android:defaultValue="#string/default_base_url"
android:summary="#string/base_url_desc"
android:title="#string/base_url_title" />
</PreferenceScreen>

android - changing ListPreference value from an Activity

Following the new API's I have created a pretty simple PreferenceFragment:
public class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
I use that in my Activity created to enable the user to change app settings:
public class SetPreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PrefsFragment()).commit();
}
}
Now I need to change the summary of a ListPreference according to user's choice. There is no findPreference() method defined for Activity class, so how can I access the desired preference?
Sorry if the answer is obvious, I'm not really familiar with the new API and always used a PreferenceActivity before it became deprecated..
Instead of doing new PrefsFragment() right in your statement where you replace the fragment you can do this.
PreferenceFragment pFrag = new PrefsFragment();
PrefsFragment pf = (PrefsFragment)pFrag;
getFragmentManager().beginTransaction()
.replace(android.R.id.content, pFrag).commit();
then in your PreferenceFragment you create a public method that you can change what you want
pf.changeMyPreference();

Categories

Resources