Failed to update header summaries for PreferenceActivity on smartphones with 4.x

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

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

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

Now, to update the title, I look 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 great on tablets, but it doesn't affect smartphones. When the user returns from the PreferenceFragment to the first PreferenceActivity (by clicking the back button), the headers remain unchanged.

+4
source share
1 answer

I solved the solution by typing a question. InvalidateHeaders () is called in the second activity (the one that contains the PrefenceFragment). It should be called in the initial activity (the one that contains the headers). This is not the most suitable solution, but I keep the link to the parent activity and onResume (). I reject the headers if the preference has been changed.

+3
source

Source: https://habr.com/ru/post/1447917/


All Articles