1

I defined a Runnable which starts after a button clicked, it changes image of a ImageView every 1 second. There is an other button in my view that when user click on that, again I start that Runnable but this time it changes image of ImageView every 2 seconds and so on.

My question is: when I click on second button, now do I have to individual thread running or it just stop first instance of Runnable and start the other instance?

Note: I read some topics about threads and Runnable in android but still the different is not clear for me. Also reading this question and it's answers head me hear to ask my own question. Java - Running a thread twice

Sorry for bad English.

Runnable part of my code:

private final Runnable mRunnable = new Runnable() {

    public void run() {
        if (mIsFlashOn) {
                if (mSwap) {
                    mImageViewBeam.setVisibility(View.VISIBLE);
                    mSwap = false;
                    mHander.postDelayed(mRunnable, 10000 / ((mStrobeCounter * 5) + 10));
                } else {
                    mImageViewBeam.setVisibility(View.GONE);
                    mSwap = true;
                    mHander.postDelayed(mRunnable, 10000 / ((mStrobeCounter * 5) + 10));
                }
        }
    }
};
Community
  • 1
  • 1
Mehrdad Salimi
  • 1,400
  • 4
  • 16
  • 31

2 Answers2

1

I think you need to use mHandler.removeCallbacks(mRunnable); this line to remove first runnable , and than start another

Hayk Petrosyan
  • 363
  • 1
  • 6
  • You solution worked like a charm, thanks. But I want o be sure what's happening. removeCallbacks remove the queue and so previous Runnable will not waste memory anymore? – Mehrdad Salimi Apr 01 '16 at 20:11
0

So from your code what have I understood is, you're creating a thread inside a while loop which is updating the ImageView over the time. If I've understood correctly then its really a bad practice to do so. I might be wrong. Please post your updating code in that case. How you're updating the ImageView?

If I'm right, try considering CountDownTimer which will serve your purpose I hope.

CountDownTimer mCountDownTimer = new CountDownTimer(howLongYouWantItToRunInMilis, intervalInMilis) {

    public void onTick(long remainingMilis) {
        // Update your ImageView here
        if (mSwap) {
            mImageViewBeam.setVisibility(View.VISIBLE);
            mSwap = false;

        } else {
            mImageViewBeam.setVisibility(View.GONE);
            mSwap = true;
        }
    }

    public void onFinish() {
        // Start the timer again from the code. 
    }

}.start();

Set the intervalInMilis when the button is clicked. Then mCountDownTimer.cancel() to cancel the timer and mCountDownTimer.start() to start it again immediately.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • thanks for answer. I am not creating a thread in while loop, I just run this line of code when first and second button clicked. mHandler.post(mRunnable); – Mehrdad Salimi Apr 01 '16 at 20:16