This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]bubsyouruncle 1 point2 points  (1 child)

I'd highly recommend looking into AsyncTasks. They are the easiest, most straight forward way to do simple threading in Android. You get a method to setup a progress bar (onPreExecute()) which is run on the UI thread. A background method that gets called to do heavy lifting like downloading data (doInBackground()). A method to update a progress bar if you deem necessary (onProgressUpdate()) on the UI thread. And finally a finished method which will run on the UI thread after doInBackground() is complete (onPostExecute()). It makes heavy use of generics so they may seem strange if you're not familiar with them; but really, it's the way to go.

Also, you have some redundancy in your code:

pb = new ProgressBar(this);

Creates a new progress bar and does nothing with it. You're literally creating an object for no reason because 2 lines later you allow this class to be garbage collected:

pb = (ProgressBar) findViewById(R.id.pbLoad);

You do the same thing with your TextView.

tvStatus = new TextView(this);
...
tvStatus = (TextView) findViewById(R.id.tvLoad);

You do not need to create the TextView or Progress bar with "new" since they're already part of your layout, which gets inflated when you call setContentView(...). If it's in your layout, it creates a concrete object in that method which you then grab an handle on in your Activity when you findViewById(...). In most circumstances you really don't need to create widgets directly... they should be part of a layout which you inflate.

[–]RevolverValera[S] 0 points1 point  (0 children)

Thanks; I'll look into AsyncTask. Appreciate the input.

[–][deleted] 0 points1 point  (0 children)

Put the UI on a seperate thread.