Reputation: 590
My application needs to read a file and process that information to build the list need by the activity. I can't find a good place to do it. What I have is in the body of the activity, in a { do the work } block. If I don't put the {...} around it, there are errors. This works, but is it the 'correct' way to do this? Is there a better way? This processing is only done once.
Thanks for your help
Cliff
Upvotes: 0
Views: 176
Reputation: 11662
It kind of depends on what exactly you are doing, but one common way to do lengthy processing is to create a subclass of AsyncTask
that does the processing in its doInBackground
method and updates the UI in its onPostExecute
method. Then, you can create an instance of the AsyncTask in onCreate
or onResume
, call its execute
method, and still have a responsive UI while the processing is occurring.
That's the way I would do it if the processing is tied to that particular Activity, such that you might want to stop the processing if the user left the Activity. If the processing were more generally useful and generated data that might be used elsewhere in the app, then I'd consider using a subclass of Service
or IntentService
.
Specifics are always helpful. If you can share more details about what you are trying to do, it will be easier to suggest the most appropriate solution.
Upvotes: 2
Reputation: 13242
You can see the Android Activity lifecycle here: http://developer.android.com/reference/android/app/Activity.html
As you can see, onCreate()
is the first thing that happens when an Activity
is launched. Depending on the size of your file, it's OK to read it in the onCreate()
method. Alternatively, you can set up your layout and then create a ProgressDialog
that shows the file loading - once it's done loading, you just dispose the dialog and build the list you want.
Upvotes: 0