-1

I have a simple personal application I'm working on that queries some records in an SQL Database and populates an adapter for a listview and is basically working fine... but I've began to wonder if I'm doing certain things at the right point of the framework.

Currently I'm loading everything up during onCreate(). In theory, I could be loading up quite a bit of data, so I wanted to possibly throw up a ProgressDialog while the information is being added to the adapter, but I ran into some odd threading issues with the Cursor. Ultimately, I launched a Progress Dialog near the end of onCreate(), followed by sleeping on another thread and calling a method to load my data with runOnUiThread() following the short sleep time, having the end of that method dismiss the Progress Dialog.

This works, but it's brought me to whether or not I should be loading database data during onCreate... or whether it should be moved to onStart() or onResume(), adding in code to clear the close and open the database, clear and repopulate the adapter as necessary as other Activity's are started and finished. Or would all that be unnecessary and I should just keep the adapter populated during onCreate()?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Maximus
  • 8,351
  • 3
  • 29
  • 37

2 Answers2

0

Move it to onResume, as if you stop the activity you can destroy the adapter and fill it back when to resume the activity. It helps to save memory and also helps to update the adapter if data has changed.

vivek
  • 284
  • 3
  • 8
  • do you have any references to support the claim "It helps to save memory and also helps to update the adapter"? Unless I don't understand something, this suggestion seems like the wrong thing to do. onResume is potentially called much more frequently than onCreate, plus it's called after every time onCreate is called. Just take a look at the Activity Lifecycle diagram. http://developer.android.com/reference/android/app/Activity.html – Thane Anthem Apr 16 '11 at 00:31
0

Reto Meier's suggestion to use an Application may suit your needs. Take a look at Activity restart on rotation Android

Community
  • 1
  • 1
Thane Anthem
  • 4,093
  • 4
  • 26
  • 24
  • I guess the main concern I have, I suppose, is when the the activity loses focus but is still running. It seems like I should be clearing the adapter to save memory. – Maximus Apr 16 '11 at 23:41
  • As I'd guess you've already read a few times, the Android system will reclaim memory that your app uses, at its own discretion. In the onDestroy method of the Activity, it may make sense to have code akin to if (myDatabaseAdapter != null) myDatabaseAdapter.close(); – Thane Anthem Apr 16 '11 at 23:50