问题
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:


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.
回答1:
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?
来源:https://stackoverflow.com/questions/10622430/duplicate-listfragment-entries-when-changing-orientation