Search code examples
androidandroid-adapterandroid-listfragment

re-creating activity clear listview item


To begin with, I am a senior student in China, so if my expression confuses you, please forgive me as my English would not be so perfect.

I have an activity with a viewPager, the viewPager contains four listFragment, which loads data from SQList database. The adapter used by listFragment is a cursorAdapter.EveryThing goes correctly until re-creating activity. I set the back button to push the activity to back stack. If the system claims more resources to run other things, the activity would be destroyed. However, when I go back to the activity , the list item in the first two listFragment will be cleared! But the last two are normal. I tested some code and I guarantee that the listView in listFragment and the cursor both initialize after re-creating, and I call

    listAdapter.changeCursor(mCursor ); //mCursor is the result from database

after query the database.

To my view, the re-creating's only difference with creating is the bundle in onCreate(). Why the first time creating the activity shows normal view but re-creating shows the strange result.

Thank you for your time. ^_^


Solution

  • Finally, I find the solution by myself. I found the problem is I call the setAdapter() in getItem() in TabAdapter.However, when re-creating the activity, I find the getItem() would not be called.However, the listFragment in TabViewPager is normal, that means, the onCreate() and onViewCreated() in listFragment will be called when re-creating the activity.

    how to fixed it: putting the setAdapter() method to the onViewCreated() in listFragment rather than the getItem().