I am trying to handle orientation changes on my Android app. I have faced a problem while trying to set adapter for my ListFragment after orientation has changed. There are no exceptions but the list remains empty; it's not filled with the given data.
I am aware that on orientation change the whole activity is recreated. I also implemented a way to pass data from the old orientation to the new one. I have checked that just before calling ListFragment.setAdapter() the ArrayList of data is not empty and it contains exactly the same data as before. I am also sure that there are no flaws in my custom adapter class because it fills the list perfectly well for the first time.
Also, I'm targeting devices lower than 3.0, so I'm using android.support.v4.app.ListFragment
to implement my ListFragment.
Here is my onCreate() method:
private ListFragment fragmentA;
private ListFragment fragmentB;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.my_activity);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = bar.newTab().setText("TabA");
ActionBar.Tab tabB = bar.newTab().setText("TabB");
fragmentA = new MyListFragment();
fragmentB = new MyListFragment();
Object retained = getLastCustomNonConfigurationInstance();
if (retained instanceof MyAsyncTask) {
ResultWrapper result = ((MyAsyncTask) retained).getResult();
Log.d("MyActivity", "retained is instance of MyAsyncTask");
if (result.nowResult == null) {
Log.d("MyActivity", "null");
} else {
Log.d("MyActivity", "nowResult size" + result.nowResult.size());
fragmentA.setListAdapter(new MyListAdapter(this,
R.layout.my_list_item, result.nowResult));
}
fragmentB.setListAdapter(new MyListAdapter(this,
R.layout.my_list_item, result.myResult));
} else {
fetcher = new MyAsyncTask(this);
fetcher.execute();
}
tabA.setTabListener(new MyTabListener(fragmentA));
tabB.setTabListener(new MyTabListener(fragmentB));
bar.addTab(tabA);
bar.addTab(tabB);
}
Any help would be appreciated.
Probably your getView method is not being called. To resolve this issue call: setListShown(true) in your ListFragment. You are obligated to do this when using the compatibility library.
Kind regards, Bram