Search code examples
androidandroid-fragmentsorientation-changesandroid-listfragment

Duplicate ListFragment entries when changing orientation


So, I'm developing an app with fragments, and everything is fine, except that when I change orientation the entries on my ListFragment duplicate like this:

NormalLandscape

And when I go back to portrait mode, the items are still duplicated; until I kill the process and restart.

To populate the list I'm using an AsyncTaskLoader, but it only runs once, when the application starts for the first time; then my adapter gets filled with the info for the rows.

I'm not using the <fragment> tag in my main.xml, the fragment is added by code in the 'parent' activity like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        TodoListFragment fragment = new TodoListFragment();
        FragmentTransaction trans = getFragmentManager().beginTransaction();
        trans.replace(R.id.fragment_list_container, fragment,
                LIST_FRAGMENT_TAG);
        trans.commit();
    }
}

The replace was and add, but changed trying to test if it made any difference, it didn't. I also tried using the setRetainInstance(true) method, to no avail.

I'm stumped, any help would be greatly appreciated.


Solution

  • You say the AsyncTaskLoader only runs when the app starts... On orientation change the app is restarting, so maybe it's running a second time?