Layout Not Refreshing After Orientation Change
Solution 1:
You add views dynamically (on user click event). By default, android does not "remember" to keep these dynamic views when re-creating the activity on configuration changes, you have to handle this process yourself.
Some possibilities :
Avoid recreating activity on screen rotation by declaring
android:configChanges="keyboardHidden|orientation|screenSize"
for your activity in AndroidManifest.xml - This is highly not recommended"Remember" what views were dynamically added when re-creating activity after rotation (for example using extra flags to detect that new filter button was added and pass it via bundle in onSaveInstanceState, and check in
onCreate
whether you need to re-create the button), or retain the whole view object as explained here
One extra note : you perhaps want to specify "vertical" orientation for your breadCrumb
layout, it is horizontal by default.
Solution 2:
I found out why this is happening.
onSave/onRestoreInstanceState I was persisting the currentFilter class which has some custom listeners on it.
As onResume method I was doing the following:
@OverrideprotectedvoidonResume() {
if (currentFilter == null) {
currentFilter = newFilterItemList();
currentFilter.addListener(newFilterItemListListener() {
@OverridepublicvoidfilterChanged(FilterChangedEvent e) {
filterCategories(categoryRepository);
}
@OverridepublicvoidfilterAdded(FilterAddedEvent e) {
FilterItem addedItem = e.getAddedItem();
baseCategories.remove(newBaseCategory("", addedItem.getSectionName()));
}
@OverridepublicvoidfilterRemoved(FilterRemovedEvent e) {
FilterItem removedItem = e.getRemovedItem();
baseCategories.add(newBaseCategory("", removedItem.getSectionName()));
}
});
}
}
The pointer to the previous instance was persisted. That's why my interface was not behaving correctly.
Now I do re-register listeners even when currentFilter is not null (it is restored) so they can point to the right instance.
Is there any pattern in handling this situations?
Thanks
Post a Comment for "Layout Not Refreshing After Orientation Change"