Reputation: 1819
I have a listview that has datasource getting from Network ( quite big list). What I'm doing is provide 2 different layout for listview item ( just move some label on that list to take advantage of width of view in landscape mode). I don't want my activity to be reloaded when I rotate my phone, so I set in Manifest file
android:configChanges="orientation"
Next, I also implement the override method onConfigurationChanged() in my Activity to help me renew my ListAdapter with new listview item ( for landscape mode ) if it's in Landscape (by checking newConfiguration.orientation). I make this listadapter, call notifyDataSetChanged() and also call listview.invalidate() to re-paint the whole listview. What I get was a big surprise: Some item are changed to new listview item( in landscape mode ) and some other listview item are not changed.
I have spent a lot of time for this and have no clue for this, please help ! Thanks in advanced.
Upvotes: 1
Views: 1620
Reputation: 8304
android:configChanges="orientation"
is very often NOT the way to go, and as I see it, that's also true for your case.
One very simple fix I did on a when I encountered a similar problem was to return your listview's data in onRetainNonConfigurationInstance()
and use that by calling getLastNonConfigurationInstance()
to repopulate your list in onCreate. You can do a null check (and an additional instanceOf to be safe) to fork your logic between loading the data from somewhere, or just casting getLastNonConfigurationInstance() to your listview's data variable.
sample code from my onCreate:
if (getLastNonConfigurationInstance() == null) {
catalogUpdater.start(); // start background task of populating your list's contents variable, in my case tempItems
} else {
tempItems = (ArrayList<WatchListItem>) getLastNonConfigurationInstance();
watchListAdapter = new WatchListItemBaseAdapter(MyActivity.this, tempItems);
...
Upvotes: 1
Reputation: 6728
Can you try doing whatever you are doing inside onConfigurationChanged() through a handler ?
Something like:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
handler.sendEmptyMessageDelayed(1,500);
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == 1) {
// do your onConfigurationChanged Stuff
}
}
};
Here I'm guessing that you might have to wait for sometime after onConfigurationChanged to update your ListView
Upvotes: 2