I have a PreferenceActivity with a bunch of (Sub)PreferenceScreens. Each such (Sub)PreferenceScreen represents an account and has the account-username as its title.
This works for me, you have to grab the underlying dialog of your PreferenceScreen and set the title from there, really easy.
somePrefScreen.getDialog().setTitle("Whatever you want");
this might be a late answer but still... I'm on it right now :)
The way I achieved it, is that you can hook into the PreferenceFragmentCompat's onResume() method of its lifecycle and manually update the required field by resetting their values.
Note : in this example I also keep track of the index of the edited Preference, just to avoid reset every single ones.
// let's say you need to update title of the parent screen
// when back from sub-screen(s) edition
private int edited = -1;
// this gets called everytime you get back to the parent screen
@Override
public void onResume ()
{
super.onResume();
if ( edited != -1 )
{
PreferenceScreen root = getPreferenceScreen();
Preference preference = root.getPreference( edited );
if ( preference != null )
{
String updatedValue = getPreferenceManager()
.getSharedPreferences()
.getString( "your-preference-key", "your-default-value" );
preference.setTitle( updatedValue );
}
edited = -1;
}
}
// everytime you are about to navigate to a sub-screen
@Override
public boolean onPreferenceTreeClick ( Preference preference )
{
// beware to save it first
if ( preference instanceof MySubScreenPreference )
{
edited = preference.getOrder();
}
return super.onPreferenceTreeClick( preference );
}
As specified in the docs :
onResume
Called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.
Which is good is that it also works of course with FragmentManager's Transactions.
Hope this helps, happy coding ! :)
I'm just putting
((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
right after updating the summary of my parent preference item.
notifyDataSetChanged() is right solution. But I want to add recursive iterator for all PreferenceScreens, as far as I got a problem to find real parent of a Preference. For complicated structure of Preferences I recommend this killer-code:
private void updateAll_PrefereneScreens(PreferenceGroup group) {
if (group instanceof PreferenceScreen) {
BaseAdapter adapter = (BaseAdapter) ((PreferenceScreen) group).getRootAdapter();
adapter.notifyDataSetChanged();
}
for (int i=0; i<group.getPreferenceCount(); i++) {
Preference pref = group.getPreference(i);
if (pref instanceof PreferenceGroup) {
updateAll_PrefereneScreens((PreferenceGroup) pref);
}
}
}
I call it after every setSummary() to ensure it works properly:
findPreference("KEY").setSummary(str);
updateAll_PrefereneScreens(getPreferenceScreen());
I found a solution to this. I have a hierarchy like this, each of these is a PreferenceScreen:
main settings
-> users list
-> user1 settings
-> user2 settings
...
In the users list, title of the sub-screen is dependent on the user settings. Now when I create the user list, I store the list adapter to a variable in my PreferenceActivity.
PreferenceScreen usersListScreen = ...
userScreenListAdapter = (BaseAdapter)usersListScreen.getRootAdapter();
Now when userX-settings are edited, I set the titles in the usersListScreen and after that call:
userScreenListAdapter.notifyDataSetChanged();
which updates the list UI and the changes are visible.
PreferenceActivity#onContentChanged()
will refresh the whole screen, occurring some flicker effect.
However you can achieve the same goal selectively on a given preference with the PreferenceActivity#onPreferenceTreeClick(...)
method.
Note that this method is now deprecated : consider using fragments that seems to solve a lot of issues with custom preferences (I've not tested myself yet). There is a compatibility package for older SDK.
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
Log.v(TAG, "onSharedPreferenceChanged(...," + key + ")");
if (key.equals("myPref")) {
onPreferenceTreeClick(getPreferenceScreen(), getPreferenceManager().findPreference("myPref"));
Log.v(TAG, "Do whatever else you need...");
}
//onContentChanged(); // this could be used but occurs screen flickering
}