Blackberry ActivityIndicator not seen during Http Connection

446 Views Asked by At

I have written the underlying code to display an activityindicator while a hhtp connection call is made. Unfortunately, the activity indicator does not appear on the screen till the http connection call is complete. Hence it is not serving its purpose.

UiApplication.getUiApplication().invokeAndWait(new Runnable()
                {
                  public void run()
                  {

                       ActivityIndicatorView view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
                        ActivityIndicatorModel model = new ActivityIndicatorModel();
                        ActivityIndicatorController controller = new ActivityIndicatorController();

                        view.setController(controller);
                        view.setModel(model);

                        controller.setModel(model);
                        controller.setView(view);

                        model.setController(controller);        

                        Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
                        view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

                        _activityFieldManager.add(view);
                          UiApplication.getUiApplication().getActiveScreen().invalidate();

                  }
                });
                //SyncWithDB is the Class that makes the Http connection call
                new SyncWithDB();
                _activityFieldManager.delete(view);
                UiApplication.getUiApplication().getActiveScreen().invalidate();
                Dialog.inform("Sync Complete");

As suggested I even tried

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
                ActivityIndicatorModel model = new ActivityIndicatorModel();
                ActivityIndicatorController controller = new ActivityIndicatorController();

                view.setController(controller);
                view.setModel(model);

                controller.setModel(model);
                controller.setView(view);

                model.setController(controller);        

                Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
                view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

                _activityFieldManager.add(view);
                  UiApplication.getUiApplication().getActiveScreen().invalidate();

                UiApplication.getUiApplication().invokeAndWait(new Runnable()
                {
                  public void run()
                  {

                      try {
                        new SyncWithDB();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                  }
                });


                _activityFieldManager.delete(view);
                UiApplication.getUiApplication().getActiveScreen().invalidate();
                Dialog.inform("Sync Complete");

But the result remains the same.

1

There are 1 best solutions below

6
On BEST ANSWER

Since you are using invokeAndWait(), are you performing the HTTP operation in a worker thread? If not, you should be. The ActivityIndicator should be managed in the main dispatch thread, and the HTTP operation managed in a worker thread.

For example:

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel model = new ActivityIndicatorModel();
ActivityIndicatorController controller = new ActivityIndicatorController();

view.setController(controller);
view.setModel(model);

controller.setModel(model);
controller.setView(view);

model.setController(controller);        

Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

_activityFieldManager.add(view);
UiApplication.getUiApplication().getActiveScreen().invalidate();

Thread thread = new Thread()
{
  public void run()
  {
    try {
      new SyncWithDB();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    UiApplication.getUiApplication().invokeLater(
      new Runnable()
      {
        protected void run()
        {
          _activityFieldManager.delete(view);
          UiApplication.getUiApplication().getActiveScreen().invalidate();
          Dialog.inform("Sync Complete");
        }
      }
    );
  }
}
thread.start();

Or, a little more generically:

ActivityIndicatorView view;
ActivityFieldManager _activityFieldManager;

class SyncFinished extends Runnable
{
  public void run()
  {
    _activityFieldManager.delete(view);
    UiApplication.getUiApplication().getActiveScreen().invalidate();
    Dialog.inform("Sync Complete");
  }
}

class SyncThread extends Thread
{
  Runnable runWhenFinished;

  public SyncThread(Runnable r)
  {
    super();
    runWhenFinished = r;
  }

  public void run()
  {
    try {
      new SyncWithDB();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (runWhenFinished != null)
      UiApplication.getUiApplication().invokeLater(runWhenFinished);
  }
}

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel model = new ActivityIndicatorModel();
ActivityIndicatorController controller = new ActivityIndicatorController();

view.setController(controller);
view.setModel(model);

controller.setModel(model);
controller.setView(view);

model.setController(controller);        

Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

_activityFieldManager.add(view);
UiApplication.getUiApplication().getActiveScreen().invalidate();

SyncThread thread = new SyncThread(new SyncFinished());
thread.start();