tags:

views:

173

answers:

1

Hi every one, I have this code in android. But when the Thread begging the loading in freezing. How I can prevent the freezing. here the code:

private void reload(final String criteria) { try { myProgressDialog = ProgressDialog.show(myfiles.this, "Please wait...", "Loading Your Photos... Please Wait", false);

new Thread() { public void run() { try {

  Thread.sleep(2000);
 } catch (Exception e) {
 }

 runOnUiThread(new Runnable() {
  @Override
  public void run() {
   // Fetch the fact

   try {
    /* here my code  */

   } catch (Exception e) {
    myProgressDialog.dismiss();
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

  }
 });
 // Dismiss the Dialog

 myProgressDialog.dismiss();
}

}.start(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }

}
A: 

By using runOnUiThread you are doing your intensive load task on the UI thread which will freeze the UI. You want to run your task in a background thread, and then on task completion update the ui.

Thread task = new Thread(new Runnable() {
    void run() {
       //load some data.
       // notify UI thread using Handler
    }
});
task.start();
BrennaSoft