1

So I come from the java world and normally when I want to loop I can just use something like:

while(true) {
    //code here till false
}

but in Android I am noticing that that kind of loop, at least in my onCreate() method causes an application to not be runnable. My loop I want to use is really short and calls some simple UI updates (changes the background color).

How can I implement a loop in Android that calls UI changes? Where do I put this loop? (clearly not onCreate) Examples/sample code would be much appreciated.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Flynn
  • 5,903
  • 7
  • 38
  • 55
  • Do you need to make UI changes periodically? – Albinoswordfish Jun 07 '11 at 17:38
  • if you are doing GUI stuff behind the scenes have you taken a look at Fragment class? http://developer.android.com/reference/android/app/Fragment.html – JPM Jun 07 '11 at 17:41
  • Not even behind the scenes, I just want it to start a loop that changes the background color and then not stop till a boolean is false – Flynn Jun 07 '11 at 17:52

4 Answers4

2

You may want to rethink your design. You don't want to performing a blocking loop like that in the onCreate() method of your activity. You are preventing any UI updates and may end up just hanging your application. You will probably want to move the loop to an AsyncTask.

Will Tate
  • 33,439
  • 9
  • 77
  • 71
2

On average, how many times does this loop execute? If it is a loop used to continually monitor something, you should probably use a new thread to do that. You could use a singleton and synchronize access to a few getters/setters to pass data back and forth.

You probably already know this, but if you use a while(true) loop, you should also have a Thread.sleep(...) in there so as not to lock down the CPU.

You could do something like this:

public class SingletonClass implements Runnable {

    private boolean someValue;

    public static final SingletonClass INSTANCE = new Test();

    private Test() {
      Thread t = new Thread(this);
      t.setDaemon(true);
      t.start();
    }

    public synchronized boolean getValue() {
      return someValue;
    }

    public synchronized void setValue(boolean value) {
      someValue = value;
    }

    @Override
    public void run() {
      while (true) {
        // ... do your work here...

        // Monitor something and set values
        setValue(true);

        Thread.sleep(500);
    }
  }
}

In your activity you could access the necessary items like this:

SingletonClass.INSTANCE.getValue()
Tanner Perrien
  • 3,133
  • 1
  • 28
  • 35
0

You also might try to look for an already existing method you can override/extend, like onDraw or onLayout. See the View documentation for further references.

icyerasor
  • 4,973
  • 1
  • 43
  • 52
0

I will recommend to use Handler and Runnable.

private Handler mHandler = new Handler();
private boolean isDone = false;
private Runnable indefinateTask = new Runnable() {
@Override
public void run() {
    if(isDone){
        mHandler.removeCallbacks(this);
    } else {
        //Perform Your Operation Over Here
        mHandler.postDelayed(indefinateTask, 1);
    }


}

};

and in onCreate call

new Thread(indefinateTask).start();

Might Help: http://developer.android.com/resources/articles/painless-threading.html

Hope this help. Cheers !!!

Gopal
  • 1,719
  • 12
  • 13