Question

I have implemented custom listview.Now the user refreshes the app from another activity which causes the database to be updated.So, now the user comes back to the activity which has custom listview.now the issue is the listview is not updated to new data set.To fix this i call oncreate in onresume in my activity which has the custom listview associated with it. But doing this causes the items in the listview to duplicate itself(means it increase the number of items and repeats itself for every refresh call). Please help!

Was it helpful?

Solution

http://developer.android.com/training/basics/activity-lifecycle/starting.html

When activity starts, onCreate(), onStart(), onResume() methods are called in sequence one by one, if you call onCreate() in onResume(), that means you are going to fill the list twice.

You can make a method such as fillList() and call it only in onResume() state, in that way, whenever your activity resumes or creates, this method will be called, But make sure that this operation is not heavy. According to android, activity starts being visible to user onStart() but after onResume() user can interact.

OTHER TIPS

As an answer to your headline, you should never call onCreate() in onResume(), because there is nothing you cannot do in the onResume() method alone.

When an app is started, not only is onCreate() called, but also onResume() right afterwards. So it will generally be the best idea to move any code you have to call when the app starts or comes to foreground again into the onResume() method.

We need more information on your exact code if this answer is not sufficient for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top