Costi Muraru
Costi Muraru

Reputation: 2065

Unable to update the header summaries for PreferenceActivity on smartphones running 4.x

From what I've noticed, on smartphones running 4.x when you have a PreferenceActivity with headers, the OS will first create an activity containing the headers. When the user clicks on one item from the headers list, another activity will be created representing the PreferenceFragment for that entry. On tablets, the headers list and this fragment belongs to the same activity and appear simultaneously on the screen.

So, the problem is this. When the user is in the PreferenceFragment, and he changes some setting there, I want to update the corresponding header summary. I have a reference to the headers object from onBuildHeaders() call:

@Override
public void onBuildHeaders(List<Header> aTarget) {
    ...
    headers = aTarget;
}

Now in order to update the header I loop through this list and check the id:

private void setHeaderSummary(int id, String summary) {
    for (Header header : headers) {
        if (header.id == id) {
            header.summary = summary;
            invalidateHeaders();
            return;
        }
    }
}

This works perfect on tablets, however on smartphones it has no effect. When the user returns from the PreferenceFragment to the first PreferenceActivity (by hitting the Back button), the headers remain unchanged.

Upvotes: 4

Views: 467

Answers (1)

Costi Muraru
Costi Muraru

Reputation: 2065

I figured the solution while typing the question. The invalidateHeaders() is called in the second activity (the one containing the PrefenceFragment). It should be called in the initial activity (the one containing the headers). It's not the prettiest solution, but I keep a reference to the parent activity and onResume() I invalidate the headers if a preference was changed.

Upvotes: 3

Related Questions