Right way to initialize preference XML for PreferenceFragmentCompat - android

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?

Related

How to create dynamic Splash Screen in Android Studio using Shared Preferences to set TextView

I'm trying to get my Android app's initial screen to update a TextView whenever a SharedPreference is changed in the settings menu, but I'm not sure exactly where to go from here.
This is a method that I added to my initial screen code to see if it would change when the preference was changed in the settings, but it never gets called according to my debugging efforts. Other parts of our application (mainly the search function) call this successfully.
public class initialScreen extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_initial_screen);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key){
String nameOfPref = "test";
if (key.equals("instance")){
nameOfPref = sharedPreferences.getString("instance","test");
}
TextView tv = (TextView) findViewById(R.id.title);
tv.setText(nameOfPref);
}
// ... plus some other StartActivity(intent) methods that don't matter here
}
Which the preference is set by a different class like this:
public class mySettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
I guess I'm trying to figure out how to make my initial screen dynamic and 'always listening' for that preference to change.

Using Multiple SharedPreferences in Android

I have an android app with its own SharedPreferences file called "settings.xml". It is stored under res folder:
public class PreferencesActivity extends PreferenceActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(my_package.R.xml.settings);
}
}
I use a library that has its own SharedPreferences file called library_settings.xml:
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(library_package.R.xml.library_settings);
}
}
However, when I deploy my app. Only one of them is copied. I am able to use my own settings.xml using:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GlobalApplication.getAppContext());
return prefs.getBoolean("some_key", true);
How do I programmatically access library_settings.xml SharedPreferences values? Also, library_settings.xml is not getting copied to the app folder i.e., /data/PACKAGE_NAME/shared_prefs/

Android - The function getLayoutResource()?

I saw this some code :
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected int getLayoutResource() {
return R.layout.activity_home;
}
....
In which they used getLayoutResource() instead of setContentView in order to add View Layout.
I don't understand, why it is so. Does getLayoutResource() before or alongside with onCreate(Bundle savedInstanceState).
I read some about getLayoutResource() (Gets the layout resource that will be shown as the View for this Preference.) in android developer document but i didn't understand .
Is there anyone to explain more.
I want to know where and where we should use getLayoutResource().
While I can't tell from your code snippet, I don't think that you are talking about Preference, since Preferences don't have onCreate (as far as I know). So, I am assuming that you are talking about a custom method named getLayoutResource in an activity. As such, getLayoutResource likely just returns the layout resource ID, without doing anything to it. The returned layout resource ID could then be used in onCreate by setContentView, which actually inflates the resource. We use it this way:
public abstract class BaseActivity {
#Override
onCreate(Bundle savedInstanceState) {
setContentView(getLayoutResource);
}
protected abstract int getLayoutResource();
}
public class MyActivity extends BaseActivity {
#Override
protected int getLayoutResource() {
return R.layout.my_activity_layout;
}
}
In short, you use getLayoutResource anywhere where you need the layout resource ID for a view.
To read more about setContentView, check out this: https://developer.android.com/reference/android/app/Activity.html#setContentView .
This answer discusses the difference between getLayoutResource and setContentView: What is the different between setContentView and getLayoutResource in android?

PreferenceManager.setDefaultValues in a PreferenceFragment

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

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

Categories

Resources