GUI freezes when adding layouts from a thread

Put your problem here if it does not fit any of the other categories.

GUI freezes when adding layouts from a thread

Postby THRESHE » Thu Dec 31, 2009 3:59 pm

Hello everyone!

I'm trying to fill a LinearLayout that is on the ScrollView with a lot of custom layouts. It takes quite a bit of time and the GUI freezes for that time. I thought that using threads would be a good solution but it doesn't work either.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         Handler handler = new Handler()
  3.  
  4.         {
  5.  
  6.                 public void handleMessage(Message msg)
  7.  
  8.                 {
  9.  
  10.                         createLayouts ();
  11.  
  12.                         addLayouts ();
  13.  
  14.                         hideSplash ();
  15.  
  16.                 }
  17.  
  18.         };
  19.  
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

The thread is launched like this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 Thread loadLayouts= new Thread (new Runnable ()
  3.  
  4.                 {
  5.  
  6.                         handler.sendEmptyMessage (1);
  7.  
  8.                 });
  9.  
  10.                 loadLayouts.setDaemon (true);
  11.  
  12.                 loadLayouts.start ();
  13.  
  14.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


This solution doesn't work :( The ProgressBar on a splash doesn't spin. Any clues?
THRESHE
Freshman
Freshman
 
Posts: 5
Joined: Sun Dec 13, 2009 11:38 am

Top

Postby jwei512 » Sun Jan 03, 2010 11:23 pm

Try taking a look at this:

http://developer.android.com/reference/ ... cTask.html

I've had a lot of success using AsyncTasks. The four steps are:

1) onPreExecute()

2) doInBackground()

3) onProgressUpdate()

4) onPostExecute()

So if you want to use a progress dialog, what I would normally do is initialize it in onPreExecute() since this is handled in the main UI thread, so it would look something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. public void onPreExecute() {
  5.  
  6.     pd = ProgressDialog.show(Activity.this,"Please Wait","Loading complex layout");
  7.  
  8. }
  9.  
  10.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


And then in your doInBackground you would do all of the loading and creating of the layouts, and if you want to update your progress dialog then send strings into the onProgressUpdate class.

And finally, in your onPostExecute, that is when you finish any rendering that you need to do in the main UI thread, and that's also where you can dismiss your progress dialog.

Hope this helps!
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby THRESHE » Sun Jan 03, 2010 11:26 pm

Thanks for your reply but I've already found the solution :) Though the spinner still freezes when I add layouts from thread.
THRESHE
Freshman
Freshman
 
Posts: 5
Joined: Sun Dec 13, 2009 11:38 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests