PreferenceScreen in PreferenceFragment closing on ScreenOrientation change - android

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.

Related

NullPointerException accessing activity from PreferenceFragment

Edit: Changing the language results in SettingsActivity.onCreate being called twice: (1) Due to recreate() and (2) due to context change.
However SettingsFragment.onCreate is being called only once, which kind of explains below question. But why isnt't SettingsFragment.onCreate being called after the second execution of the SettingsActivity.onCreate?
I am quite new to Android development and want to understand the reason which makes my application crash:
Within a settings activity I have placed a PreferenceFragment which allows the user to change the UI language. In order to take effect and not to spoil the back stack I call the recreate() method of the fragment's activity.
The first implementation looked like below:
(A)
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingsFragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
getFragmentManager().executePendingTransactions();
fragment.setActivity(this);
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener
{
Activity m_activity;
public void setActivity(Activity activity){
m_activity = activity;
}
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences preferences = getPreferenceScreen().getSharedPreferences();
preferences.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
...
m_activity.recreate();
}
}
}
Sometimes above solution works, sometimes m_activity is Null and the application crahes. As a possible alternative solution I removed the 'setActivity' setter and called 'getActivity' within onSharedPreferenceChanged:
(B)
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
...
getActivity().recreate();
}
But this also results in sporadic NullPointerExceptions.
In scenario (B) I found that the fragment's 'onAttach' is not being called for any reason sometimes and hence 'getActivity' returns null. However I would expect scenario (A) not to crash since this isn't Null in onCreate?
Problem solved!
For those who have the same problem: I had to unregister the onSharedPreferenceChanged listener in SettingsFragment.onDestroy.

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

Toolbar with AppCompatPreferenceActivity AND PreferenceHeaders

I'd like to use a Toolbar on my Settings activity, which is using AppCompatPreferenceActivity. There's plenty of stuff online for doing this if you're just using PreferenceScreen, but I haven't found anything that also works with PreferenceHeaders. Here's my current code:
public class SettingsActivity extends AppCompatPreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}
I also have some unremarkable PreferenceHeaders in an XML file, which have corresponding PreferenceFragments. The activity runs fine, I just can't figure out how to get the toolbar in there.
Most of the stuff I've seen on this usually involves just a single PreferenceScreen, and you create a layout with the toolbar and a placeholder ListView, and load the Preferences into there, but that doesn't work for me.
I also tried getSupportActionBar(), but that returns null.
Anyone know how I can do this? Thanks!

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

Load/Save settings from/to database in PreferenceFragment

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.

Categories

Resources